本文介绍了转换后访问原始响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

即使请求有错误,我的后端也会返回200个HTTP代码:

I've a backend that returns 200 HTTP code even if the request had errors:

{
    "error": {
        "message": "Generic error",
        "code": 13000
    }
}

现在,在使用后,如何访问原始响应正文:

Now, how can I access raw response body, after using:

.addConverterFactory(GsonConverterFactory.create(gson))

我尝试过:

response.raw().body().string()

但是我得到了

java.lang.IllegalStateException: Cannot read raw response body of a converted body.

我已经实现了一个通用的APICallback类,该类应阻止"成功并引发APIError事件:

I've implemented a generic APICallback class, that should "block" the success and fire an APIError event:

abstract class APICallback<T> implements Callback<T> {

    abstract void onSuccess(Call<T> call, T result);

    @Override
    public void onResponse(Call<T> call, Response<T> response) {
        if (response.body() != null) {

            if (response.body() instanceof APIError) {
                // this is not working
            } else {
                onSuccess(call, response.body());
            }
        } else {
            apiError = new APIError("Unknown error");
            bus.post(new APIErrorEvent(apiError));
        }
    }

    @Override
    public void onFailure(@NonNull Call<T> call, @NonNull Throwable t) {
        String message = t.getLocalizedMessage() != null ? t.getLocalizedMessage() : "Unknown error";
        apiError = new APIError(message);
        bus.post(new APIErrorEvent(apiError, source));
    }
}

我想保留自动转换器"功能...在此先感谢.

I'd like to mantain the "auto-converter" capability...many thanks in advance.

推荐答案

对于错误响应,您必须使用response.errorBody()而不是response.body()

For error response you have to take response.errorBody() not response.body()

abstract class APICallback<T> implements Callback<T> {

abstract void onSuccess(Call<T> call, T result);

@Override
public void onResponse(Call<T> call, Response<T> response) {
    if (response.body() != null) {

        if (response.body() instanceof APIError) {
            // this is not working
        } else {
            onSuccess(call, response.body());
        }
    } else {
        apiError = new APIError("Unknown error");
        bus.post(new APIErrorEvent(apiError));
    }
}

@Override
public void onFailure(@NonNull Call<T> call, @NonNull Throwable t) {
    String message = t.getLocalizedMessage() != null ? t.getLocalizedMessage() : "Unknown error";
    apiError = new APIError(message);
    bus.post(new APIErrorEvent(apiError, source));
}
}

更改为

abstract class APICallback<T> implements Callback<T> {

    abstract void onSuccess(Call<T> call, T result);

    @Override
    public void onResponse(Call<T> call, Response<T> response) {
        if (response.isSuccessful()) {
            if (response.body() != null) {
                onSuccess(call, response.body());

            }
        } else {
            if (response.errorBody() != null) {

                if (response.errorBody() instanceof APIError) {

                }
            } else {
                apiError = new APIError("Unknown error");
                bus.post(new APIErrorEvent(apiError));
            }
        }

    }

@Override
public void onFailure(@NonNull Call<T> call, @NonNull Throwable t) {
    String message = t.getLocalizedMessage() != null ? t.getLocalizedMessage() : "Unknown error";
    apiError = new APIError(message);
    bus.post(new APIErrorEvent(apiError, source));
}
 }

这篇关于转换后访问原始响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-30 07:37