改造2 - 空响应体 [英] Retrofit 2 - null response body

查看:252
本文介绍了改造2 - 空响应体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想下面的响应与转换改造2

{
    "errorNumber":4,
    "status":0,
    "message":"G\u00f6nderilen de\u011ferler kontrol edilmeli",
    "validate":[
        "Daha \u00f6nceden bu email ile kay\u0131t olunmu\u015f. L\u00fctfen giri\u015f yapmay\u0131 deneyiniz."
    ]
}

但我得到永诺在 onResponse 方法响应。所以,我想看看与 response.errorBody.string响应的错误体()。错误体内含有与原料反应完全相同的内容。

But I am allways getting null response in onResponse method. So I tried to look at error body of the response with response.errorBody.string(). Error body contains exactly same content with raw response.

下面是我的服务方法,改造对象和响应数据declerations:

Here is my service method, Retrofit object and response data declerations:

@FormUrlEncoded
@POST("/Register")
@Headers("Content-Type: application/x-www-form-urlencoded")
Call<RegisterResponse> register(
        @Field("fullName")  String fullName,
        @Field("email")     String email,
        @Field("password")  String password);

public class RegisterResponse {
    public int status;
    public String message;
    public int errorNumber;
    public List<String> validate;
}

OkHttpClient client = new OkHttpClient();
client.interceptors().add(new Interceptor() {
    @Override
    public Response intercept(Chain chain) throws IOException {
        Response response = chain.proceed(chain.request());
        final String content = UtilityMethods.convertResponseToString(response);
        Log.d(TAG, lastCalledMethodName + " - " + content);
        return response.newBuilder().body(ResponseBody.create(response.body().contentType(), content)).build();
    }
});
Retrofit retrofit = new Retrofit.Builder()
        .baseUrl(BASE_URL)
        .addConverterFactory(GsonConverterFactory.create())
        .client(client)
        .build();
domainSearchWebServices = retrofit.create(DomainSearchWebServices.class);

我已经控制响应 JSON 使用 jsonschema2pojo 来看看我modled我响应类赖特,似乎确定。

I have controlled response JSON with jsonschema2pojo to see if I modled my response class wright and it seems OK.

为什么要改造未能我的反应转换?

Why Retrofit fails to convert my response?

更新

现在作为一个工作围绕我建设从错误我的身体反应。

For now as a work around I am building my response from error body.

推荐答案

我已经解决了这个问题。当我做一个坏的请求(HTTP 400)改造不转换的响应。在这种情况下,您可以访问 response.errorBody.string原始响应()。之后,你可以创建一个新GSON并手动将它转换:

I have solved the problem. When I make a bad request (HTTP 400) Retrofit doesn't convert the response. In this case you can access the raw response with response.errorBody.string(). After that you can create a new Gson and convert it manually:

if (response.code() == 400 ) {
    Log.d(TAG, "onResponse - Status : " + response.code());
    Gson gson = new Gson();
    TypeAdapter<RegisterResponse> adapter = gson.getAdapter(RegisterResponse.class);
    try {
        if (response.errorBody() != null)
            registerResponse = 
                adapter.fromJson(
                    response.errorBody().string());
    } catch (IOException e) {
        e.printStackTrace();
    }
}

这篇关于改造2 - 空响应体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆