Rxjava2 - 多个改造请求并处理所有异常

时间:2017-03-16 16:03:55

标签: android retrofit2 rx-java2

我是Rxjava的新手。它太复杂了......

我需要处理一个复杂的场景,我想知道如何使用Rxjava。

这是关于应用启动的逻辑:

  1. 应用启动并splash页面打开
  2. 制作http reqesut check_update以检查应用是否需要更新。
  3. 如果应用有新版本且必须更新,请弹出一个只有一个update按钮的通知对话框;如果应用有新版本但不必更新,则弹出一个包含2个按钮的对话框(updateignore);如果没有新版本,请转到下一步。
  4. 无需更新,因此我们需要根据当前home状态跳转到login页面或auth_token页面。
  5. 从sharedPreference中读取auth_token。然后调用http请求check_auth来检查它是否有效。如果有效,请跳至home页面;如果没有,请跳至login页。
  6. 在此过程中,涉及2个http请求和大量条件检查。还需要处理http请求错误,此过程是否可以用Rxjava编写?

    我想我可以这样做,但我不确定它是否正确。

    mRetrofitService.checkUpdate(new CheckUpdateRequest("android", "0.0.1")) // check update request
                .compose(RxUtil.applyScheduler()) // scheduler
                .flatMap(RxUtil.applyFunction()) // handle the response.
                .flatMap((Function<CheckUpdateResponse, ObservableSource<?>>) checkUpdateResponse -> {
                    int updateMode;
                    if (checkUpdateResponse.isHas_new_version()) {
                        if (checkUpdateResponse.isForce_update()) {
                            updateMode = FORCE_UPDATE; // 1
                        } else {
                            updateMode = CHOOSE_UPDATE; // 2
                        }
                    }  else {
                        updateMode = NO_UPDATE; // 0
                    }
                    return Observable.just(updateMode);
                })
                .flatMap(o -> {
                    if (Integer.parseInt(o.toString()) == NO_UPDATE) {
                        return mRetrofitService.checkAuth();
                    }
                    return null; //what should I return for pop up dialog?
                })
                .flatMap(RxUtil.applyFunction())
                .subscribe(new Observer<CheckAuthResponse>() {
                    @Override
                    public void onSubscribe(Disposable d) {
    
                    }
    
                    @Override
                    public void onNext(CheckAuthResponse checkAuthResponse) {
                        // valid
                        gotoHome();
                    }
    
                    @Override
                    public void onError(Throwable e) {
                        // should I handle pop here?
                        // should I handle auth invalid here?
                        // should I handle checkUpdate request error here?
                        // should I handle checkAuth here?
                        // how to distinguish these errors?
                    }
    
                    @Override
                    public void onComplete() {
    
                    }
                });
    

    我是对的吗?你能否回答我的问题(代码中的评论)?

1 个答案:

答案 0 :(得分:0)

根据文档onError“通知观察者Observable遇到了错误情况。”因此,此回调与处理请求错误无关。

我建议您使用Response类改造来处理不同的情况。

例如,您可以像这样定义您的服务:

@method(url)
Call<CheckAuthResponse> checkUpdate(...);

并在你的rx工作流程中:

mRetrofitService.checkUpdate(new CheckUpdateRequest("android", "0.0.1")) // check update request
            .compose(RxUtil.applyScheduler()) // scheduler
            .flatMap(RxUtil.applyFunction()) // handle the response.
            .map((Function<Response<CheckUpdateResponse>, CheckUpdateResponse>) retrofitResponse -> {
                if ( retrofitResponce.code() == authInvalidCode ) {
                  //do something
                 } else if (...) {

                 }
                 return retrofitResponse.body()
            })
            .flatMap((Function<CheckUpdateResponse, ObservableSource<?>>) checkUpdateResponse -> {
                int updateMode;
                if (checkUpdateResponse.isHas_new_version()) {
                    if (checkUpdateResponse.isForce_update()) {
                        updateMode = FORCE_UPDATE; // 1
                    } else {
                        updateMode = CHOOSE_UPDATE; // 2
                    }
                }  else {
                    updateMode = NO_UPDATE; // 0
                }
                return Observable.just(updateMode);
            })
            .flatMap(o -> {
                if (Integer.parseInt(o.toString()) == NO_UPDATE) {
                    return mRetrofitService.checkAuth();
                }
                return null; //what should I return for pop up dialog?
            })
            .flatMap(RxUtil.applyFunction())
            .subscribe(new Observer<CheckAuthResponse>() {
                @Override
                public void onSubscribe(Disposable d) {

                }

                @Override
                public void onNext(CheckAuthResponse checkAuthResponse) {
                    // valid
                    gotoHome();
                }

                @Override
                public void onError(Throwable e) {
                    // should I handle pop here?
                    // should I handle auth invalid here?
                    // should I handle checkUpdate request error here?
                    // should I handle checkAuth here?
                    // how to distinguish these errors?
                }

                @Override
                public void onComplete() {

                }
            });

希望这会有所帮助;

抱歉我的英文。

相关问题