前段时间有人给我建议,Retrofit应该加上同意错误处理以及返回Call的包装,今天抽时间就在这里简单的记录记录。。。。。
关于Retrofit前面的东西就不说了,主要是对请求的操作统一做个处理。去前章理理思路
- 我先拿俩个串看下
//串①
{code:200,msg:null,data:{userId:1,userName:"aa",fullName:"富民燃气管理员",companyId:1,companyName:"虎门能源",mobileModules:[]}}
//串②
{code:200,msg:null,data:{company:{storeName:null,storeCode:null,saleDate:"2018-01-25",todayOrderNum:957,todayOrderBottleNum:1170,todayTotalAmount:117642,yesterdayGrowthOrderNum:-54,yesterdayGrowthOrderBottleNum:-128,yesterdayGrowthTotalAmount:-22203,saleDetailInfos:[{materialTypeName:"15KG",orderNum:764,orderBottleNum:948,totalAmount:97366},{materialTypeName:"50KG",orderNum:21,orderBottleNum:39,totalAmount:12978},{materialTypeName:"5KG",orderNum:172,orderBottleNum:183,totalAmount:7298}]},stores:[{storeName:"龙眼站",storeCode:"1010",saleDate:"2018-01-25",todayOrderNum:67,todayOrderBottleNum:75,todayTotalAmount:6567,yesterdayGrowthOrderNum:0,yesterdayGrowthOrderBottleNum:0,yesterdayGrowthTotalAmount:0,saleDetailInfos:[{materialTypeName:"15KG",orderNum:45,orderBottleNum:51,totalAmount:5295},{materialTypeName:"50KG",orderNum:1,orderBottleNum:1,totalAmount:352},{materialTypeName:"5KG",orderNum:21,orderBottleNum:23,totalAmount:920}]}]}}
在上面返回的JSON串中可以得出他们有共同的部分吧(都是请求结果返回),那好,可以这样:
public class BaseHttpRequestBean<T> {
/**
* code : 200
* msg : null
* data : {"userId":1,"userName":"aa","fullName":"富民燃气管理员","companyId":1,"companyName":"虎门能源","mobileModules":[]}
*/
private int code;
private String msg;
//定义成泛型所有公用
private T data;
public int getCode() {
return code;
}
public void setCode(int code) {
this.code = code;
}
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
public T getData() {
return data;
}
public void setData(T data) {
this.data = data;
}
}
- 在对我们所有的请求结果做一个处理(预处理),以前的时候,对请求结果我们也做过处理是这样的
//添加Gson转换器
new Retrofit.Builder().addConverterFactory(GsonConverterFactory.create(gson));
- 现在我们修改下
GsonConverterFactory
,可以自定义一个MyGsonConverterFactory
继承Converter.Factory
,只修改他responseBodyConverter
和requestBodyConverter
方法即可
public class MyGsonConverterFactory extends Converter.Factory {
/**
* Create an instance using a default {@link Gson} instance for conversion. Encoding to JSON and
* decoding from JSON (when no charset is specified by a header) will use UTF-8.
*/
public static MyGsonConverterFactory create() {
return create(new Gson());
}
/**
* Create an instance using {@code gson} for conversion. Encoding to JSON and
* decoding from JSON (when no charset is specified by a header) will use UTF-8.
*/
public static MyGsonConverterFactory create(Gson gson) {
return new MyGsonConverterFactory(gson);
}
private final Gson gson;
private MyGsonConverterFactory(Gson gson) {
if (gson == null) throw new NullPointerException("gson == null");
this.gson = gson;
}
@Override
public Converter<ResponseBody, ?> responseBodyConverter(Type type, Annotation[] annotations,
Retrofit retrofit) {
return new MyGsonRequestBodyConverter<>(gson, type);
}
@Override
public Converter<?, RequestBody> requestBodyConverter(Type type,
Annotation[] parameterAnnotations, Annotation[] methodAnnotations, Retrofit retrofit) {
return new MyGsonRequestBodyConverter<>(gson, type);
}
}
MyGsonRequestBodyConverter.class
public class MyGsonRequestBodyConverter<T> implements Converter<ResponseBody, T> {
private final Gson gson;
private final Type type;
MyGsonRequestBodyConverter(Gson gson, Type type) {
this.gson = gson;
this.type = type;
}
@Override
public T convert(ResponseBody value) throws IOException {
String response = value.string();
BaseHttpRequestBean baseHttpRequestBean = gson.fromJson(response, BaseHttpRequestBean.class);
//请求结果进行统一的预处理
/**
* 请求返回码为:
* 200 --- 请求成功
* 301 --- 无此用户
* 302 --- 密码错误
* ... --- 未知错误
*/
if (baseHttpRequestBean.getCode() == 301) {
throw new ErrorApi(301);
} else if (baseHttpRequestBean.getCode() == 302) {
throw new ErrorApi(302);
} else if (baseHttpRequestBean.getCode() == 200) {
return gson.fromJson(response, type);
} else {
throw new ErrorApi(300);
}
}
}
- 统一错误返回码处理类
ErrorApi
public class ErrorApi extends RuntimeException {
public static final int USER_NOT_EXIST = 301;//该用户不存在
public static final int WRONG_PASSWORD = 302;//密码错误
public ErrorApi(int resultCode) {
this(getApiExceptionMessage(resultCode));
}
public ErrorApi(String detailMessage) {
super(detailMessage);
}
private static String getApiExceptionMessage(int code) {
String mes = "";
switch (code) {
case USER_NOT_EXIST:
mes = "账户不存在";
break;
case WRONG_PASSWORD:
mes = "密码错误";
break;
default:
mes = "未知错误";
}
return mes;
}
}
- 现在回到前面,可以定义我们自己的APi接口啦,回到上章内容
AnApiService
接口文件中,这样
/**
* 登陆
*
* @param username
* @param password
* @return
*/
@POST("login/loginByAccount?")
Observable<HttpResult<LoginDataBean>> logins(@Query("username") String username, @Query("password") String password);
/**
* 获取销量数据
*
* @return
*/
@POST("sale/getTodaySaleInfoByUserId")
Observable<BaseHttpRequestBean<SalesDataBean>> getTodaySaleInfoByUserIds();
- 好了,现在在回到
ApiServiceManger
中,看看这次的请求怎么搞
/**
* 登录
* @param observer
* @param phone
* @param password
*/
public void logins(Subscriber<LoginDataBean> observer, String phone, String password) {
anApiService.logins(phone, password)
.map(new BaseHttpRequestBean<LoginDataBean>())
.subscribeOn(Schedulers.io())//指定 subscribe() 发生在 IO 线程
.observeOn(AndroidSchedulers.mainThread()).subscribe(observer);// 指定 Subscriber 的回调发生在主线程
}
HttpResult()<>
是用来统一出处理请求返回,将BaseHttpRequestBean<>
中包含的T
取出,(此处T = LoginDataBean
),最后可以根据返回做预处理,并且返回data
部分
private class HttpResultFunc<T> implements Func1<BaseHttpRequestBean<T>, T> {
@Override
public T call(BaseHttpRequestBean<T> httpResult) {
if (httpResult.getCode() == 301) {
throw new ErrorApi(301);
} else if (httpResult.getCode() == 302) {
throw new ErrorApi(302);
} else if (httpResult.getCode() == 200) {
return httpResult.getData();
} else {
throw new ErrorApi(100);
}
}
}
- 最后的就直接上代码了,是自定义的对外的接口,以及请求结果统一处理
public interface SubscriberOnNextListener<T> {
void onNext(T t);
}
// 可自定义Dialog实现,在开始请求和结束请求时,可分别显示Dialog提醒用户
public class MySubscriber<T> extends Subscriber<T> {
private SubscriberOnNextListener mSubscriberOnNextListener;
private Context context;
public MySubscriber(SubscriberOnNextListener mSubscriberOnNextListener, Context context) {
this.mSubscriberOnNextListener = mSubscriberOnNextListener;
this.context = context;
}
/**
* 开始时调用
*/
@Override
public void onStart() {
super.onStart();
//此处可自定义显示Dialog
}
@Override
public void onCompleted() {
}
/**
* 错误处理
*
* @param e
*/
@Override
public void onError(Throwable e) {
}
/**
* 将onNext方法中的结果返回
*
* @param t
*/
@Override
public void onNext(T t) {
if (mSubscriberOnNextListener != null) {
mSubscriberOnNextListener.onNext(t);
}
}
}
- 请求
AnApiServiceManger.getInstance().
logins(new MySubscriber<LoginDataBean>(new SubscriberOnNextListener<LoginDataBean>() {
@Override
public void onNext(LoginDataBean loginDataBean) {
Log.d(TAG, loginDataBean.getCompanyName() + "=\n=" + loginDataBean.getFullName()
+ "=\n=" + loginDataBean.getCompanyId() + "=\n=" + loginDataBean.getUserName());
}
}), str1, edPasstext.getText().toString().trim());
好了,就这样可以简简单单的将统一处理封装起来了,请求连接就不提供了,因为是公司服务器,不属于个人不能共享。。。。。,欢迎反馈,有问题请热情告知
网友评论