まず、あなたの要求のためのクラスを作成し、例えば、LoginRequest.java
第二
public class LoginRequest {
private String email;
private String username;
private String password;
//getters and setters
}
は、あなたの応答のためのクラスを作成し、あなたの中LoginResponse.java
最後に
public class LoginResponse {
private Integer status;
private String Message;
private Boolean data;
//getters and setters
}
、インターフェイスこのメソッドを追加:
public interface MiApiInterface {
@POST("yourResourceName") Call<LoginResponse> login(@Body LoginRequest request);
}
私はそれがあなたを助けることができます希望あなたに質問があるかどうか私に尋ねてください。
は、ログインメソッドの戻り値は、それはあなたがあなたの活動でこのようにそれを使用することができ、非同期呼び出し用で、コールであることに気づいてい:、まず
改造インスタンスを作成
Retrofit retrofit = ....
第二に、このようなあなたのインターフェイスのインスタンスを作成します。
:
MiApiInterface apiInterface = retrofit.create(MiApiInterface.class);
最後に、あなたがloginメソッドにアクセスすることができました0
LoginRequest request = new LoginRequest();
request.set();
....
Call<LoginResponse> responseCall = apiInterface.login(request);
responseCall.enqueue(new Callback<LoginResponse>() {
public void onResponse(...){
LoginResponse loginResponse = response.body();
}
public void onFailure(...){
}
}
自動的に、あなたの改修ビルダーにコンバータファクトリーを追加する必要がありますJSONにオブジェクトを変換するには:
Gson gson = new GsonBuilder().create();
Retrofit.Builder()
.baseUrl(baseUrl)
.addConverterFactory(GsonConverterFactory.create(gson))
...
はあなたのGradleにGsonライブラリをインポート忘れてはいけません。
これはおそらくあなたが探しているものです:http:// stackoverflow。com/questions/21398598/how-to-post-raw-whole-json-in-a-retrofit-request –
[Retrofitリクエストの本文にJSON全体をPOSTする方法は? ](http://stackoverflow.com/questions/21398598/how-to-post-raw-whole-json-in-the-body-of-a-retrofit-request) –