Retrofit+RxJava最佳封装使用
2016-07-27 本文已影响5504人
水大云霄
说来惭愧,Retrofit已经流行一段时间了,我个人还未对这个square出品的强大框架进行学习http://square.github.io/retrofit/。 最近趁着空闲撸了一把,看了网上很多大神博客加上自己的总结,写了一个Retrofit+RxJava的网络请求工具封装,自己感觉还是比较全面的,包括OKHttp拦截实现缓存,打印请求log信息,添加头部等功能,用起来还是挺酸爽的,有不足之处请个人大神指教。
首先对我们一般app应用的请求进行进行解析,一般我们得请求是这样的:
{"code":0,"desc":"success","content":{} }
那么我们可以写一个通用的HttpResult返回结构体 ,当然结构体跟写接口的哥们讨论好,不然让你蛋疼 :
public class HttpResult<T> {
public int code;
public String desc;
public T content;
public boolean isSuccess() {
return code == Constant.SUCCESS;
}
public boolean isEmpty() {
return code == Constant.EMPTY;
}
public boolean isNoMore() {
return code == Constant.NOMORE;
}
}
接着,我们再写一个类,用于封装所有请求API:APIService
/**
* Created by hjzhang on 2016/7/26.
*/
public interface APIService {
@GET("GetDayDiscountList")
Observable<HttpResult<ListDayDiscount>> getDayDiscount(@Query("param") String param);
}
然后,再写一个总的工具类,用来配置Retrofit和OKHttp,以及公用的一些Rx方法,注释详细,请look代码:
/**
* Created by hjzhang on 2016/7/26.
*/
public class RetrofitHttpUtil {
/**
* 服务器地址
*/
private static final String BASE_URL = "http://app.aishangh.com/Id_Index.asmx/";
public APIService apiService;
private static Retrofit retrofit = null;
private static OkHttpClient okHttpClient = null;
private Context mContext;
//缓存设置0不缓存
private boolean isUseCache;
private int maxCacheTime = 60;
public void setMaxCacheTime(int maxCacheTime) {
this.maxCacheTime = maxCacheTime;
}
public void setUseCache(boolean useCache) {
isUseCache = useCache;
}
public APIService getService() {
if (apiService == null && retrofit != null) {
apiService = retrofit.create(APIService.class);
}
return apiService;
}
public void init(Context context) {
this.mContext = context;
initOkHttp();
initRetrofit();
if (apiService == null) {
apiService = retrofit.create(APIService.class);
}
}
private void initOkHttp() {
OkHttpClient.Builder builder = new OkHttpClient.Builder();
//打印请求log日志
if (BuildConfig.DEBUG) {
HttpLoggingInterceptor loggingInterceptor = new HttpLoggingInterceptor();
loggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
builder.addInterceptor(loggingInterceptor);
}
// 缓存 http://www.jianshu.com/p/93153b34310e
File cacheFile = new File(AppUtil.getCacheDir(mContext), "httpCache");
// Log.d("OkHttp", "缓存目录---" + cacheFile.getAbsolutePath());
Cache cache = new Cache(cacheFile, 1024 * 1024 * 50);
Interceptor cacheInterceptor = new Interceptor() {
@Override
public Response intercept(Chain chain) throws IOException {
Request request = chain.request();
if (!AppUtil.isNetworkConnected(mContext)||isUseCache) {//如果网络不可用或者设置只用网络
request = request.newBuilder()
.cacheControl(CacheControl.FORCE_CACHE)
.build();
// Log.d("OkHttp", "网络不可用请求拦截");
} else if(AppUtil.isNetworkConnected(mContext)&&!isUseCache){//网络可用
request = request.newBuilder()
.cacheControl(CacheControl.FORCE_NETWORK)
.build();
// Log.d("OkHttp", "网络可用请求拦截");
}
Response response = chain.proceed(request);
if (AppUtil.isNetworkConnected(mContext)) {//如果网络可用
// Log.d("OkHttp", "网络可用响应拦截");
response = response.newBuilder()
//覆盖服务器响应头的Cache-Control,用我们自己的,因为服务器响应回来的可能不支持缓存
.header("Cache-Control", "public,max-age="+maxCacheTime)
.removeHeader("Pragma")
.build();
} else {
// Log.d("OkHttp","网络不可用响应拦截");
// int maxStale = 60 * 60 * 24 * 28; // tolerate 4-weeks stale
// response= response.newBuilder()
// .header("Cache-Control", "public, only-if-cached, max-stale=" + maxStale)
// .removeHeader("Pragma")
// .build();
}
return response;
}
};
builder.cache(cache);
builder.interceptors().add(cacheInterceptor);//添加本地缓存拦截器,用来拦截本地缓存
builder.networkInterceptors().add(cacheInterceptor);//添加网络拦截器,用来拦截网络数据
//设置头部
// Interceptor headerInterceptor = new Interceptor() {
// @Override
// public Response intercept(Chain chain) throws IOException {
// Request originalRequest = chain.request();
// Request.Builder requestBuilder = originalRequest.newBuilder()
// .header("myhead", "myhead")
// .header("Content-Type", "application/json")
// .header("Accept", "application/json")
// .method(originalRequest.method(), originalRequest.body());
// Request request = requestBuilder.build();
// return chain.proceed(request);
// }
// };
// builder.addInterceptor(headerInterceptor );
//设置超时
builder.connectTimeout(15, TimeUnit.SECONDS);
builder.readTimeout(20, TimeUnit.SECONDS);
builder.writeTimeout(20, TimeUnit.SECONDS);
//错误重连
builder.retryOnConnectionFailure(true);
okHttpClient = builder.build();
}
private void initRetrofit() {
retrofit = new Retrofit.Builder()
.baseUrl(BASE_URL)
.client(okHttpClient)
.addConverterFactory(GsonConverterFactory.create())
.addCallAdapterFactory(RxJavaCallAdapterFactory.create())
.build();
}
public <T> void toSubscribe(Observable<T> o, Subscriber<T> s) {
o.subscribeOn(Schedulers.io())
.unsubscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(s);
}
/**
* 用来统一处理Http的resultCode,并将HttpResult的Data部分剥离出来返回给subscriber
*
* @param <T> Subscriber真正需要的数据类型,也就是Data部分的数据类型
*/
public class HttpResultFunc<T> implements Func1<HttpResult<T>, T> {
@Override
public T call(HttpResult<T> httpResult) {
if (!httpResult.isSuccess()) {
throw new APIException(httpResult.code, httpResult.desc);
}
return httpResult.content;
}
}
}
toSubscribe方法是公用的RxJava线程切换方法,网络请求在Schedulers.io()线程,而结果处理切换到AndroidSchedulers.mainThread()线程,妥妥的来去自如。
HttpResultFunc方法是处理返回结果的,有个自定义异常处理,除了有正确数据以外,其余转移给APIException处理。
/**
* 自定义异常
* Created by hjzhang on 2016/7/26.
*/
public class APIException extends RuntimeException{
public int code;
public String message;
public APIException(int code, String message) {
this.code = code;
this.message = message;
}
@Override
public String getMessage() {
return message;
}
public int getCode() {
return code;
}
}
其中,log拦截器,缓存拦截器代码请看源码吧。
再接着,写一个工厂类,去实际产生调用处理结果,继承RetrofitHttpUtil ,对应APIService:APIFactory
public class APIFactory extends RetrofitHttpUtil{
public static APIFactory getInstance() {
return SingletonHolder.INSTANCE;
}
private static class SingletonHolder {
private static final APIFactory INSTANCE = new APIFactory();
}
public void getDayDiscount(Subscriber<ListDayDiscount> subscriber, String param){
Observable observable = apiService.getDayDiscount(param)
.map(new HttpResultFunc<ListDayDiscount>());
toSubscribe(observable, subscriber);
}
}
最后,我们调起需求的时候,需要一个等待加载的dialog,把dialog调起和结束封装金subscriber中,同样代码说话:
/**
* 用于在Http请求开始时,自动显示一个ProgressDialog
* 在Http请求结束是,关闭ProgressDialog
* 调用者自己对请求数据进行处理
* Created by hjzhang on 16/7/26.
*/
public class ProgressSubscriber<T> extends Subscriber<T> implements ProgressCancelListener{
private SubscriberOnNextListener mSubscriberOnNextListener;
private ProgressDialogHandler mProgressDialogHandler;
private Context context;
public ProgressSubscriber(SubscriberOnNextListener mSubscriberOnNextListener, Context context,boolean show) {
this.mSubscriberOnNextListener = mSubscriberOnNextListener;
this.context = context;
mProgressDialogHandler = new ProgressDialogHandler(context, this, true,show);
}
private void showProgressDialog(){
if (mProgressDialogHandler != null) {
mProgressDialogHandler.obtainMessage(ProgressDialogHandler.SHOW_PROGRESS_DIALOG).sendToTarget();
}
}
private void dismissProgressDialog(){
if (mProgressDialogHandler != null) {
mProgressDialogHandler.obtainMessage(ProgressDialogHandler.DISMISS_PROGRESS_DIALOG).sendToTarget();
mProgressDialogHandler = null;
}
}
/**
* 订阅开始时调用
* 显示ProgressDialog
*/
@Override
public void onStart() {
showProgressDialog();
}
/**
* 完成,隐藏ProgressDialog
*/
@Override
public void onCompleted() {
dismissProgressDialog();
}
/**
* 对错误进行统一处理
* 隐藏ProgressDialog
* @param e
*/
@Override
public void onError(Throwable e) {
if (e instanceof SocketTimeoutException) {
if (mSubscriberOnNextListener != null) {
mSubscriberOnNextListener.onError(Constant.NETERROR,"网络中断,请检查您的网络状态");
}
} else if (e instanceof ConnectException) {
if (mSubscriberOnNextListener != null) {
mSubscriberOnNextListener.onError(Constant.NETERROR,"网络中断,请检查您的网络状态");
}
} else if(e instanceof APIException){
if (mSubscriberOnNextListener != null) {
mSubscriberOnNextListener.onError(((APIException) e).getCode(),((APIException) e).getMessage());
}
}else{
if (mSubscriberOnNextListener != null) {
mSubscriberOnNextListener.onError(Constant.UNKONWERROR,e.getMessage());
}
}
dismissProgressDialog();
}
/**
* 将onNext方法中的返回结果交给Activity或Fragment自己处理
*
* @param t 创建Subscriber时的泛型类型
*/
@Override
public void onNext(T t) {
if (mSubscriberOnNextListener != null) {
mSubscriberOnNextListener.onNext(t);
}
}
/**
* 取消ProgressDialog的时候,取消对observable的订阅,同时也取消了http请求
*/
@Override
public void onCancelProgress() {
if (!this.isUnsubscribed()) {
this.unsubscribe();
}
}
}
差不多封装完成,看一个例子:
public class MainActivity extends BaseActivity {
private TextView tv_content;
private Button bt_get;
private SubscriberOnNextListener getResultOnNext;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv_content = (TextView) findViewById(R.id.tv_content);
bt_get = (Button) findViewById(R.id.bt_get);
bt_get.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
retrofitUtil.setUseCache(false);
retrofitUtil.getDayDiscount(new ProgressSubscriber(getResultOnNext, MainActivity.this,true),"{\"pageIndex\":1,\"pageSize\":10,\"version\":1}");
}
});
getResultOnNext = new SubscriberOnNextListener<ListDayDiscount>() {
@Override
public void onNext(ListDayDiscount result) {
StringBuilder sb = new StringBuilder();
if(result!=null){
for (ProductBean bean : result.getListDayDiscount()){
sb.append(bean.getTeam_Title()).append("||");
}
}
tv_content.setText(sb.toString());
}
@Override
public void onError(int code, String message) {
Toast.makeText(MainActivity.this,code+"||"+message,Toast.LENGTH_SHORT).show();
}
};
}
}