我是新來的改造。我怎樣才能發送參數,並從波紋管網址獲取Json?在android中翻新?
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命中字段或JSON響應的形式,還是直接在URL中輸入? –
我的資料庫是:compile'com.squareup.retrofit2:retrofit:2.1.0' compile('com.squareup.retrofit2:converter-gson:2.0.0-beta4'){ exclude module:'retrofit' } –
我不是在談論改造。如果你可以分享實際的API鏈接,我可以幫你解決它 –