[英]Receiving NULL on response error body if the status code is 400 with retrofit in android
[英]Retrofit2.0.0 beta getting null response body ,response code is 400
我是Retrofit的新手,我嘗試在我的項目中實現Retrofit 2.0.0 beta。 為了注冊用戶,我嘗試了Retrofit。 METHOD POST是,Content Type是application/x-www-form-urlencoded
。 我得到的響應為400,但響應正文為null
。 我在這里附上我的代碼。
public interface MyApiInterface {
@Headers("Content-Type: application/x-www-form-urlencoded")
@FormUrlEncoded
@POST("/users/register")
Call<LoginResponse> doRegister(@Field("key") String value, @Field("key")
String value;
}
public class RestClient {
private static Retrofit REST_CLIENT;
private static MyApiInterface apiService;
public static String BASE_URL = "http://xx.xxx.xxx.xx:";
static {
setupRestClient();
}
public static MyApiInterface get() {
return apiService;
}
private static void setupRestClient() {
REST_CLIENT = new Retrofit.Builder()
.baseUrl(BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.build();
apiService =
REST_CLIENT.create(MyApiInterface.class);
}
}
Call<Register> call = RestClient.get().doRegister("xxx",""xxxx);
call.enqueue(new Callback<Register>() {
@Override
public void onResponse(Response<Register> response, Retrofit retrofit) {
int statusCode = response.code();
LoginResponse user = response.body();
Log.d("String",statusCode+""+response.body() );
}
@Override
public void onFailure(Throwable t) {
}
});
我得到回應
{
status: "failed"
error_code: "invalid_client"
}
我的Register類是
public class Register {
public String status;
public String error_code;
}
您的后端似乎不支持默認的User-Agent
標頭,並且需要特定的標頭。 如果您未明確提供此標頭,則我認為OkHttp將添加默認的User-Agent
,它是okhttp/version
(對此不確定(可以是System.getProperty("http.agent")
))。
您應該創建Interceptor
,它將添加服務器將接受的客戶端標頭。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.