若是沒有了解過基本用法的,能夠先看一下上篇博客
Retrofit與Rxjava封裝終結者(一)基本用法,先看一下封裝以前的代碼,java
Map<String, String> map = new HashMap<>();
map.put("_t", PrefUtils.getString(mContext, "token", ""));
RxRequest.getInstance()
.getProxy(RxUrl.class)
.getServiceType(map)
.subscribeOn(Schedulers.io())
.compose(bindToLifecycle())//綁定生命週期
.compose(bindUntilEvent(ActivityEvent.DESTROY))//Destroy時銷燬
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Subscriber<ArrayList<ServiceBean>>() {
@Override
public void onCompleted() {
//TODO 完成的回調
}
@Override
public void onError(Throwable e) {
//TODO 失敗的回調
}
@Override
public void onNext(ArrayList<ServiceBean> serviceBeen) {
//TODO 成功的回調
Log.d(TAG, "onSuccessResult: " + serviceBeen.size());
mBeanList = serviceBeen;
getPeople();
if (isFirstClass) {
isFirstClass = false;
}
}
});複製代碼
.subscribe(new Action1<ArrayList<ServiceBean>>() {
@Override
public void call(ArrayList<ServiceBean> serviceBeen) {
//TODO 成功的回調
Log.d(TAG, "onSuccessResult: " + serviceBeen.size());
mBeanList = serviceBeen;
getPeople();
if (isFirstClass) {
isFirstClass = false;
}
}
});複製代碼
.subscribe(new Action1<ArrayList<ServiceBean>>() {
@Override
public void call(ArrayList<ServiceBean> serviceBeen) {
//TODO 成功的回調
Log.d(TAG, "onSuccessResult: " + serviceBeen.size());
mBeanList = serviceBeen;
getPeople();
if (isFirstClass) {
isFirstClass = false;
}
}
}, new Action1<Throwable>() {
@Override
public void call(Throwable throwable) {
//TODO 失敗的回調
}
});複製代碼
1.針對生命週期以及多餘的代碼,也很好解決,這些都是經過Observable來實現的,因此每次咱們建立一個Observable對象以後,統一進行處理git
if (observable != null)
observable.compose(subscriber.getActivity().bindToLifecycle())//綁定生命週期
.compose(subscriber.getActivity().bindUntilEvent(ActivityEvent.DESTROY))
.subscribeOn(Schedulers.io())//操做線程
.unsubscribeOn(Schedulers.io())//解綁線程
.observeOn(AndroidSchedulers.mainThread())//回調線程
.subscribe(subscriber);複製代碼
2.關於緩存,咱們須要一個標記爲,由於用戶發起請求以前咱們是不知道是否進行緩存的,因此增長了一個方法getProxy(),經過傳遞布爾值來設置是否進行緩存github
public RxUrl getProxy(boolean isCache) {
RxUrl mRxUrl = null;
if (isCache) {
mRxUrl = getCacheRetrofit().create(RxUrl.class);
} else {
mRxUrl = getRetrofit().create(RxUrl.class);
}
//若是不須要token值失效後自動刷新的需求,此處能夠直接返回RxUrl
return (RxUrl) Proxy.newProxyInstance(RxUrl.class.getClassLoader(), new Class<?>[]{RxUrl.class}, new ProxyHandler(mRxUrl, false));
}複製代碼
回調的抽象類緩存
public abstract class Callback<T> {
public abstract void onSuccess(T t);
public void onError(Throwable e) {
Log.d("net_error---->", e.toString());
}
}複製代碼
自定義的Subcriberbash
@SuppressWarnings("unchecked")
public class RxSubscriber<T> extends Subscriber<T> {
private SoftReference<Callback> rxListener;
private SoftReference<RxAppCompatActivity> mActivity;
//構造方法傳入當前的RxAppCompatActivity和回調的抽象類
public RxSubscriber(RxAppCompatActivity rxAppCompatActivity, Callback<T> listener) {
this.mActivity = new SoftReference(rxAppCompatActivity);
this.rxListener = new SoftReference(listener);
}
@Override
public void onStart() {
//TODO 但是作一些初始化操做,好比說談一個對話框或者進度條
}
@Override
public void onNext(T t) {
if (rxListener.get() != null) {
rxListener.get().onSuccess(t);
}
}
@Override
public void onCompleted() {
//TODO 請求完成時走此方法
}
@Override
public void onError(Throwable e) {
// TODO 請求發生錯誤時走此方法
if (rxListener.get() != null) {
rxListener.get().onError(e);
}
}
}複製代碼
上面寫了比較多,總結起來就是3個類網絡
最終的使用方法框架
HashMap<String, String> hashMap = new HashMap<>();
hashMap.put("strDate", "2017-03-25");//構造參數
Observable<OneBean> weather=RxRequest.getInstance().getProxy(false).postData(hashMap);//建立Observable對象
RxSubscriber subscriber = new RxSubscriber(this, new Callback<OneBean>() {
@Override
public void onSuccess(OneBean oneBean) {
tvData.setText(oneBean.getHpEntity().getStrContent());
}
});//建立Rxsubscrber對象
RequestManager.getInstance().sendRequest(weather, subscriber);//發送請求複製代碼
框架下載地址ide