Rx-Volley 自己来封装
自从15年接触了RxJava,对函数式编程越发的喜爱。以前Android项目上网络层都是统一的使用Volley,已经对网络请求的回调,多个回调嵌入各种不爽了,趁着年前任务轻松,赶紧的将Volley封装一下,投向RxJava的怀抱!!
Volley除了提供异步处理回调接口外,还支持同步调用网络,主要是使用RequestFuture进行同步调用。所以Volley封装成Rx-Volley只需要以此为入口即可。
为了方便维护和升级,已将代码提交到git:https://github.com/cmlbeliever/RX-Volley
核心代码如下:
package com.cml.lib.rxvolley.rxvolley; import com.android.volley.DefaultRetryPolicy; import com.android.volley.Request; import com.android.volley.RequestQueue; import com.android.volley.RetryPolicy; import com.android.volley.toolbox.RequestFuture; import com.cml.lib.rxvolley.framework.RequestManager; import rx.Observable; import rx.Subscriber; import rx.schedulers.Schedulers; /** * Created by cmlBeliever on 2016/2/1. * <p> * 将Volley请求封装成RxJava结构返回,注意:所有的请求都在IO线程中处理 */ public class RxRequest { /** * 发送post请求 * * @param url * @param target * @param <T> * @return */ public static <T> Observable<T> post(String url, Class<?> target) { return request(url, target, Request.Method.POST, new DefaultRetryPolicy()); } /** * 发送post请求 * * @param url * @param target * @param <T> * @return */ public static <T> Observable<T> post(String url, Class<?> target, RetryPolicy retryPolicy) { return request(url, target, Request.Method.POST, retryPolicy); } /** * 发送Get请求 * * @param url * @param target * @param <T> * @return */ public static <T> Observable<T> get(String url, Class<?> target) { return request(url, target, Request.Method.GET, new DefaultRetryPolicy()); } /** * 发送Get请求 * * @param url * @param target * @param <T> * @return */ public static <T> Observable<T> get(String url, Class<?> target, RetryPolicy retryPolicy) { return request(url, target, Request.Method.GET, retryPolicy); } public static <T> Observable<T> request(String url, Class<?> target, int method, RetryPolicy retryPolicy) { final RequestFuture<T> requestFuture = RequestFuture.newFuture(); final GsonRequest<T> request = new GsonRequest<T>(target, method, url, null, requestFuture, requestFuture); request.setRetryPolicy(retryPolicy); request.setTag(url); requestFuture.setRequest(request); return Observable.create(new Observable.OnSubscribe<T>() { @Override public void call(Subscriber<? super T> subscriber) { try { //只在被订阅后才进行网络请求处理 RequestManager.getRequestQueue().add(request); if (!subscriber.isUnsubscribed() && !requestFuture.isCancelled()) { subscriber.onNext(requestFuture.get()); subscriber.onCompleted(); } } catch (Exception e) { subscriber.onError(e); } } }).subscribeOn(Schedulers.io()); } /** * 取消请求 * * @param url */ public static void cancel(final String url) { RequestManager.getRequestQueue().cancelAll(new RequestQueue.RequestFilter() { @Override public boolean apply(Request<?> request) { return request.getTag().equals(url); } }); } }
String url = "https://raw.githubusercontent.com/cml8655/note/master/data/test.json"; RxRequest.<MyModel>post(url, MyModel.class).subscribe(new Subscriber<MyModel>() { @Override public void onCompleted() { Log.i("RxRequest", "onCompleted"); } @Override public void onError(Throwable e) { Log.e("RxRequest", "onError", e); } @Override public void onNext(MyModel myModel) { Log.i("RxRequest", "onNext==>" + myModel); } });