package com.jyc99.wealthmanager.utils;
import com.google.gson.Gson;
import com.jyc99.wealthmanager.model.TradeSimpleResult;
import retrofit2.HttpException;
/**
* Created by zeng on 2018/11/14.
*/
public class ErrorHandler {
private static String data = "{\"Success\": false,\"StatusCode\": 500,\"Message\": \"处理失败\", \"ErrorInfo\": {\"ErrorMessage\": \"网络请求错误\",\"ErrorCode\": \"404\" },\"Result\": null}";
public static TradeSimpleResult handle(Throwable throwable) {
if (throwable instanceof HttpException) {
HttpException error = (HttpException) throwable;
try {
String string = error.response().errorBody().string();
if (isJSONValid(string)) {
return new Gson().fromJson(string, TradeSimpleResult.class);
} else {
return new Gson().fromJson(data, TradeSimpleResult.class);
}
} catch (Exception e) {
e.printStackTrace();
return null;
}
} else {
throwable.printStackTrace();
return null;
}
}
public static String errorMessage(Throwable e) {
TradeSimpleResult errBody = ErrorHandler.handle(e);
if (errBody != null) {
String errorCode = errBody.getErrorInfo().getErrorCode();
if ("401".equals(errorCode)) {
//token失效了
} else if ("402".equals(errorCode)) {
} else if ("403".equals(errorCode)) {
} else if ("404".equals(errorCode)) {
}else if ("500".equals(errorCode)){
}
return errBody.getErrorInfo().getErrorMessage();
}
return "";
}
public final static boolean isJSONValid(String jsonInString) {
try {
return true;
} catch (Exception e) {
return false;
}
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
Retrofit2网络请求异常统一处理.zip (3个子文件)
Retrofit2网络请求异常统一处理
ErrorHandler.java 2KB
MainActivity.java 2KB
TradeSimpleResult.java 2KB
共 3 条
- 1
资源评论
- qq_412771942019-10-14没什么有,浪费了
weitao_666
- 粉丝: 33
- 资源: 7
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功