RxAndroid呼叫一个接一个的网络呼叫

时间:2017-07-07 13:38:27

标签: android rx-java rx-android

我是RxJava的新手。我有一个场景,我想首先登录webservice(getLoginObservable),并且成功时,想要调用另一个webservice(getFetchDataObservable)来获取用户信息。

如果登录成功,我可以使用以下代码。但我无法弄清楚如何编码failure案例。

private void doLogin() {
    emailAddress = editTextUsername.getText().toString();
    final String password = editTextPassword.getText().toString();
    showProgress(null, getString(R.string.loggingInPleaseWait));

    getLoginObservable(editTextUsername.getText().toString(), password)
            .map(response -> {
                if (response.result) {
                    getPresenter().saveUserDetails(getContext(), emailAddress, true, response.dataObject.questionId, response.dataObject.question);
                }
                return response;
            })
            .flatMap(response -> {
                return getFetchDataObservable();
            })
            .subscribe(res -> {
                dismissProgress();
                if (res.result) {
                    saveInformation(password, res);
                } else {
                    ConstantsMethods.showOkButtonDialog(getContext(), res.message, null);
                }
            }, e -> {
                dismissProgress();
                if (e instanceof NoInternetConnectionException) {
                    ConstantsMethods.showOkButtonDialog(getContext(), getString(R.string.noInternetConnection), null);
                }
                Log.e(LoginFragment.class.getSimpleName(), e.getMessage());
            });
}



    private Observable<WsResponse<SecurityQuestion>> getLoginObservable(String userName, String password) {
        return Observable.<WsResponse<SecurityQuestion>>create(subscriber -> {
            getPresenter().doLogin(getActivity(), userName, password, appType,
                    new Callback<Void, WsResponse<SecurityQuestion>>() {
                        @Override
                        public Void callback(final WsResponse<SecurityQuestion> param) {
                            subscriber.onNext(param);
                            return null;
                        }
                    });
        });
    }

    private Observable<WsResponse<PatientDataProfile>> getFetchDataObservable() {
        return Observable.create(subscriber -> {
            new AfPatientsPresenter().fetchPatientData(getContext(), emailAddress, "", new Callback<Void, WsResponse<PatientDataProfile>>() {
                @Override
                public Void callback(WsResponse<PatientDataProfile> param1) {
                    subscriber.onNext(param1);
                    subscriber.onComplete();
                    return null;
                }
            });
        });
    }

尽管我知道RxJava,但我可以发现getLoginObservable(editTextUsername.getText().toString(), password)可观察发送对地图(map(response -> { ... })的响应,并且此地图返回对flatmap(flatMap(response -> { ... })的响应并发送其响应订阅者。在这里,我很遗憾,如果登录失败,我如何跳过(第二次网络呼叫)flatmap flatMap(response -> { ... }直接向订阅者发送响应。

1 个答案:

答案 0 :(得分:3)

而不是:

.map(response -> {
                if (response.result) {
                    getPresenter().saveUserDetails(getContext(), emailAddress, true, response.dataObject.questionId, response.dataObject.question);
                }
                return response;
            })

你可以使用:

flatMap(response-> {
    if (response.result) {
        getPresenter().saveUserDetails(getContext(), emailAddress, true, response.dataObject.questionId, response.dataObject.question);
        return Observable.just(response);
    } else {
        return Observable.error(new Exception("Login failed")); // or maybe some LoginFailedException() you can reuse
    }
})