[英]Retrofit2.0.0 beta getting null response body ,response code is 400
I am new to Retrofit.I tried to implement Retrofit 2.0.0 beta in my project. 我是Retrofit的新手,我尝试在我的项目中实现Retrofit 2.0.0 beta。 To register a user i tried Retrofit.
为了注册用户,我尝试了Retrofit。 The METHOD POST is and Content Type is
application/x-www-form-urlencoded
. METHOD POST是,Content Type是
application/x-www-form-urlencoded
。 I am getting response as 400 but response body is null
. 我得到的响应为400,但响应正文为
null
。 I am attaching my code here. 我在这里附上我的代码。
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) {
}
});
I am getting response as 我得到回应
{
status: "failed"
error_code: "invalid_client"
}
and my Register class is 我的Register类是
public class Register {
public String status;
public String error_code;
}
Your backend seems to does not support default User-Agent
header and requires specific one. 您的后端似乎不支持默认的
User-Agent
标头,并且需要特定的标头。 If you do not provide this header explicitly, I think OkHttp will add default User-Agent
which is okhttp/version
(not sure about this, could be System.getProperty("http.agent")
). 如果您未明确提供此标头,则我认为OkHttp将添加默认的
User-Agent
,它是okhttp/version
(对此不确定(可以是System.getProperty("http.agent")
))。
You should create Interceptor
which will add the client header that your server will accept. 您应该创建
Interceptor
,它将添加服务器将接受的客户端标头。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.