【发布时间】:2017-02-13 11:08:54
【问题描述】:
我是 Retrofit 的新手。如何发送参数并从以下网址获取 Json?
http://xxx:8087/courier.svc/login?username=jim&password=123456
我需要一个教程链接。
此代码在我的MainActivity 中:
private void loadJSON(String username, String password) {
Retrofit retrofit = new Retrofit.Builder()
.baseUrl("http://192.168.8.11:8087/sample.svc/")
.addConverterFactory(GsonConverterFactory.create())
.build();
RequestInterface_Login request = retrofit.create(RequestInterface_Login.class);
Call<JSONResponseLogin> call = request.getJSON(username, password);
call.enqueue(new Callback<JSONResponseLogin>() {
@Override
public void onResponse(Call<JSONResponseLogin> call, Response<JSONResponseLogin> response) {
JSONResponseLogin jsonResponse = response.body();
data = new ArrayList<>(Arrays.asList(jsonResponse.getLogin()));
}
@Override
public void onFailure(Call<JSONResponseLogin> call, Throwable t) {
Log.d("Error", t.getMessage());
}
});
}
我的ModelLogin:
public class ModelLogin {
private String username;
private String password;
public String getUsername() {
return username;
}
public String getPassword() {
return password;
}
}
我的RequestInterface_Login:
public interface RequestInterface_Login {
@GET("/login/{username}/{password}")
Call<JSONResponseLogin> getJSON(@Path("username") String username, @Path("password") String password);
}
我的JSONResponseLogin:
public class JSONResponseLogin {
private ModelLogin[] login;
public ModelLogin[] getLogin() {
return login;
}
}
但是找我NullPointerException!
我从下面的服务中得到json:
{"Key":null,"Response":1}
【问题讨论】:
-
API Hit 是 field 还是 JSON Response 的形式还是直接在 URL 中输入?
-
我的库是: compile 'com.squareup.retrofit2:retrofit:2.1.0' compile ('com.squareup.retrofit2:converter-gson:2.0.0-beta4') { exclude module: “改造”}
-
我不是在谈论改造。如果您可以分享实际的 API 链接,我可以为您提供帮助
-
看看这个文档。 https://futurestud.io/tutorials/android-basic-authentication-with-retrofit 还有很多基本的例子还有如何处理http参数
-
@kshitij 耆那教。我编辑了我的问题。
标签: android retrofit retrofit2