1
Retrofit 2.0を使用してPOSTリクエストを作成しようとしましたが、いくつか問題があります。私はどんな助けにもうれしいだろう。 私はこのインタフェースを持っている:POST Request Retrofit 2.0
public interface HZApi {
@POST("URL")
Call<RespBody> register(@Body ReqBody request);
}
これは私の要求と応答のクラスです:
public class ReqBody {
@SerializedName("login")
String login;
@SerializedName("password")
String password;
public ReqBody(String login, String password){
this.login = login;
this.password = password;
}
}
public class RespBody {
@SerializedName("login")
String login;
@SerializedName("password")
String password;
@SerializedName("isreg")
int isreg;
}
そして、私の活動のコードの一部:
Retrofit retrofit = new Retrofit.Builder()
.baseUrl("http://URL/")
.addConverterFactory(GsonConverterFactory.create())
.build();
HZApi api = retrofit.create(HZApi.class);
ReqBody req = new ReqBody(login,password);
Call<RespBody> call = api.register(req);
call.enqueue(new Callback<RespBody>() {
@Override
public void onResponse(Call<RespBody> call, Response<RespBody> response) {
Toast.makeText(getApplicationContext(),"OK",Toast.LENGTH_SHORT).show();
//answer.setText(response.body().toString());
}
@Override
public void onFailure(Call<RespBody> call, Throwable t) {
Toast.makeText(getApplicationContext(),t.getMessage(),Toast.LENGTH_SHORT).show();
Log.d("MyTag",call.toString(),t);
}
});
そして私は、要求を作ってみます、JSON throw: BEGIN_OBJECTが必要ですが、1行目の2行目のBEGIN_ARRAYでした。$
私は特別なクラスなしで、ただ1つの文字列引数を使用してリクエストを作成しようとしましたが、動作しません。あなたのすべての返信ありがとうございます。
Ruslanは正しいですかあなたのサーバーが戻ってきたJSONをチェックし、それに基づいてRespBodyを作成する必要があります –
@RustemMuzafarovは、ログを有効にして、実際にサーバーに送信されていることを確認しようとします。ロギングを有効にするには、この簡単な[instruction](https://futurestud.io/blog/retrofit-2-log-requests-and-responses)を読んでください。データが実際にサーバーに送信されている場合は、そのサーバー側の問題を意味します。あなたのリクエスト宣言は私にとっては大丈夫です。 – Ruslan
あなたのお手伝いをしていただきありがとうございます。私は解決策を見つけました - 私は@FormUrlEncoded注釈を私の要求に使う必要がありました。 –