1. 封装BaseObservable
public class BaseObservable {
/**
* @param observable
* @param <T>
* @return
*/
protected <T> Observable<T> observeable(Observable<T> observable) {
return observable
.subscribeOn(Schedulers.io())
.unsubscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread());
}
}
2. 接口设置
/*当前城市*/
@POST(Urls.GETNowCity)
Observable<BaseRespons<OpenCityBean>> nowCity(@QueryMap TokenMap<String, Object> map);
3. Rx统一处理处理服务器返回
/**
* user:lqm
* desc:Rx处理服务器返回,
* 服务器的返回的数据格式一般都是一致的,所有我们每个网络请求都可以使
* 用compose(RxResultHelper.handleResult())来处理服务器返回,一般服务器返回成功码为200,
* 相应改一下返回码的判断就行了
*/
public class RxResultHelper {
private static final int RESPONSE_SUCCESS_CODE = 1000;
private static final int RESPONSE_TOKEN_ERROR = 1001;
private static final int RESPONSE_CHECK_ADDID_DELET = 1003;
private static final int RESPONSE_DISCONUT_ERROR=1006;
private static final int RESPONSE_FROZEN=1005;
public static <T> ObservableTransformer<BaseRespons<T>, T> handleResult() {
return new ObservableTransformer<BaseRespons<T>, T>() {
@Override
public ObservableSource<T> apply(Observable<BaseRespons<T>> tObservable) {
return tObservable.flatMap(
new Function<BaseRespons<T>, Observable<T>>() {
@Override
public Observable<T> apply(BaseRespons<T> tResponseData) throws Exception {
//可以相应更改
if (tResponseData.getCode() == RESPONSE_SUCCESS_CODE) {
return Observable.just(tResponseData.getData());
} else if (tResponseData.getCode() == RESPONSE_TOKEN_ERROR) {
/* Intent intent=new Intent(context, RegistActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK );
context.startActivity(intent);
return Observable.empty();*/
return Observable.error(new Exception(tResponseData.getMsg()));
}else if(tResponseData.getCode()==RESPONSE_CHECK_ADDID_DELET){
return Observable.error(new Exception("1003"));
}else if(tResponseData.getCode()==RESPONSE_DISCONUT_ERROR){
return Observable.error(new Exception("1006"));
} else if(tResponseData.getCode()==RESPONSE_FROZEN){
Intent intent=new Intent(App.getInstance().getmContext(), LoginActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK );
App.getInstance().getmContext().startActivity(intent);
SPUtils.put( App.getInstance().getmContext(), "member_id", "");
SPUtils.put( App.getInstance().getmContext(), "token", "");
return Observable.empty();
}else {
return Observable.error(new Exception(tResponseData.getMsg()));
}
}
}
);
}
};
}
private AlertDialog logout;//退出dialog
private void logoutUser() {
logout = new AlertDialog.Builder(App.getmContext())
.setContentView(R.layout.dialog_delet_and_exit)
.setCancelable(true)
.setWidthAndHeight(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT)
.show();
Button cancle = logout.getView(R.id.m_btn_cancle);
Button sure = logout.getView(R.id.m_btn_sure);
TextView title = logout.getView(R.id.m_tv_title_delet);
title.setText(R.string.check_addr_isexistence);
cancle.setOnClickListener(new OnMultiClickListener() {
@Override
public void onMultiClick(View v) {
logout.dismiss();
}
});
sure.setOnClickListener(new OnMultiClickListener() {
@Override
public void onMultiClick(View v) {
logout.dismiss();
}
});
}
}
4.在.Activity中使用
/*根据纬度坐标获取当前城市*/
public Observable<OpenCityBean> getnowCity(TokenMap<String, Object> tokenMap) {
return observeable(mService.nowCity(tokenMap)).compose(RxResultHelper.<OpenCityBean>handleResult());
}