Retrofitの新機能です。 paramを送信してJsonをurlowから取得するにはどうすればよいですか?アンドロイドでの改造?
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' コンパイル( 'com.squareup.retrofit2:converter-gson:2.0.0-beta4'){ 除外モジュール: 'retrofit' } –
私は改造について話していません。実際のAPIリンクを共有できる場合は、 –