登录失效的统一处理

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());
    }

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值