Retrofit(九) 错误处理

通常服务端返回的JSON数据格式是这样的:

  {
    "code":0,
    "msg":"successfully",
    "result":{"name":"alan","ago":18,"gender":"male"}
  }

其中的code字段是业务逻辑的错误码。

  • code = 0, 表示成功, 非0代表错误
  • code = 1000, 表示没有权限
  • code = 1001, 表示尚未注册
  • 等等

 

那么该如何去组织解析呢?

把服务端返回的error code, 统一封装成YourAppGlobleException, 抛出去,让UI或业务层去处理错误。

我是这样定义的:

public class ResponseInfo<T> implements Serializable {
    @SerializedName("code")
    private int code;
    @SerializedName("msg")
    private String message;
    @SerializedName("result")
    private T result;

    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public T getResult() {
        return result;
    }

    public void setResult(T result) {
        this.result = result;
    }
}

在接口定义中可以这样写:

public interface IDeviceService {
    @POST("v1/info/pair")
    Call<ResponseInfo<Device>> pair(@HeaderMap Map<String, String> heads, @Body Device device);
}

那如何知道成功了,即使错误了,又该怎么统一处理呢?

    protected <T> T get(Call<ResponseInfo<T>> call) throws YourAppGlobleGlobleException{
        try {
            Response<ResponseInfo<T>> response = call.execute();
            ResponseInfo<T> responseInfo = response.body();
            if (responseInfo != null && responseInfo.getCode() != 0) {
                Log4j.warn(this.getClass().getSimpleName() + ", response: " + responseInfo.getCode() + "message: " + responseInfo.getMessage());
                throw new YourAppGlobleGlobleException(responseInfo.getCode(), responseInfo.getMessage());
            }

            if (responseInfo == null) {
                Log4j.warn(this.getClass().getSimpleName() + "responseInfo == null, ErrorConnectFailed");
                throw new YourAppGlobleGlobleException(ErrorConnectFailed, "");
            }

            return responseInfo.getResult();
        } catch (IOException e) {
            Log4j.warn(this.getClass().getSimpleName() + "ErrorConnectFailed" + e.getMessage());
            throw new YourAppGlobleGlobleException(ErrorConnectFailed, "");
        }
    }
    public Device pair(Device device) {
        Call<ResponseInfo<Device>> response = mService.pair(DefaultHeader, device);
        Device targetDevice = null;
        try {
            targetDevice = get(response);
        } catch (YourAppGlobleException e) {
            e.printStackTrace();
        }



        return targetDevice ;
    }

 

上面是同步情况的错误处理,异步怎么做呢?把上面的内容封装在一个自定义的回调里面。

public abstract class AsyncCallback<T> implements Callback<ResponseInfo<T>> {

    public abstract void onFailure(YourAppGlobleException e);

    public abstract void onResponse(T result);

    @Override
    public void onResponse(Call call, Response response) {
            ResponseInfo<T> responseInfo = (ResponseInfo<T>) response.body();
            if (responseInfo != null && responseInfo.getCode() != 0) {
                onFailure(new YourAppGlobleException(responseInfo.getCode(), responseInfo.getMessage()));
                return;
            }

            if (responseInfo == null) {
                onFailure(new YourAppGlobleException(ErrorConnectFailed, ""));
                return;
            }

            onResponse(responseInfo.getResult());
    }

    @Override
    public void onFailure(Call call, Throwable t) {
        onFailure(new YourAppGlobleException(ErrorConnectFailed, t.getMessage()));
    }
}

 

    public void pairAsync(AsyncCallback<Device> callback, Device device) {
        Call<ResponseInfo<Device>> call = mService.pair(DefaultHeader, device);
        call.enqueue(callback);
    }

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值