【问题标题】:Retrofit Add custom object in Error callback. onFailure改造在错误回调中添加自定义对象。失败
【发布时间】:2016-08-31 13:21:51
【问题描述】:

你好我正在使用改造我的回调如下

       @Override
public void onResponse(final Call<T> call, Response<T> response) {

    if (response.isSuccessful()) {
       passing this to my view

    } else {


 //      as this failed other then 200      retroCallback.onFailure(call, new Throwable(""));

    }
}

@Override
public void onFailure(Call<T> call, Throwable t) {
    retroCallback.onFailure(call, t);
}

所以在这我如何传递我的 ErrorBean 而不是 Throwable 无论如何我们可以在 onFailure 中传递自定义模型?当我的服务器以某种格式给我响应时,我想通过这种格式.. 我正在使用改造 2.1.0

【问题讨论】:

标签: android retrofit retrofit2


【解决方案1】:

您可以继承 Throwable 并使用组合传递其他对象。

public class ErrorBean extends Throwable {
    public ErrorPayload payload = null;

    public ErrorBean(ErrorPayload payload) {
        this.payload = payload;
    }
}

然后,在 onError 中:

@Override
public void onFailure(Call<T> call, Throwable t) {
    retroCallback.onFailure(call, t);
    if (t instanceof ErrorBean) {
        // do your stuff here
        ((ErrorBean)t).payload.text;
    }
}

【讨论】:

    【解决方案2】:

    AFAIK,, Retrofit 的 onFailure 用于处理无网络连接等错误。

    为了处理来自您的服务器的错误响应,错误响应,我的意思是来自服务器的响应带有 4xx 状态码,但带有一些 JSON 响应供客户端处理。

    说,你从服务器得到这个错误结构:

    {
        statusCode: 409,
        message: "Email address already registered"
    }
    

    此错误将在onResponse(...) 中捕获。为了解决这个问题,创建你的

    public class ErrorBean {
    
        private int statusCode;
        private String message;
    
        public ErrorBean() {
        }
    
        public int status() {
            return statusCode;
        }
    
        public String message() {
            return message;
        }
    }
    

    创建一个简单的ErrorHandler util:

    public class ErrorUtils {
    
        public static ErrorBean parseError(Response<?> response) {
            Converter<ResponseBody, ErrorBean> converter = 
                    ServiceGenerator.retrofit()
                            .responseBodyConverter(ErrorBean.class, new Annotation[0]);
    
            ErrorBean error;
    
            try {
                error = converter.convert(response.errorBody());
            } catch (IOException e) {
                return new ErrorBean();
            }
    
            return error;
        }
    }
    

    最后,

    ...
    call.enqueue(new Callback<SuccessResponse>() {  
        @Override
        public void onResponse(Call<SuccessResponse> call, Response<SuccessResponse> response) {
            if (response.isSuccessful()) {
                // use response data and do some fancy stuff :)
            } else {
                // parse the response body …
                ErrorBean error = ErrorUtils.parseError(response);
                // … and use it to show error information
    
                // … or just log the issue like we’re doing :)
                Log.d("error message", error.message());
            }
        }
    
        @Override
        public void onFailure(Call<User> call, Throwable t) {
            // there is more than just a failing request (like: no internet connection)
        }
    });
    

    希望你明白了..!!!

    【讨论】:

    • 所以我做了一个从回调 扩展的通用类,所以基本上我想在该通用类中进行所有计算,然后通过。所以以你的方式我必须在我所有的 api 调用中执行 if (response.isSuccessful()) {} 和
    猜你喜欢
    • 2018-03-23
    • 1970-01-01
    • 2019-09-20
    • 1970-01-01
    • 1970-01-01
    • 2013-04-21
    • 2017-10-29
    • 2018-12-20
    • 1970-01-01
    相关资源
    最近更新 更多