2017-07-07 54 views
0

我是RxJava的新手。我有一個場景,我想調用第一個登錄web服務(getLoginObservable),如果成功,想調用另一個webservice(getFetchDataObservable)來獲取用戶信息。RxAndroid接連撥打一個網絡電話

如果登錄成功,我有以下代碼工作。但我無法弄清楚如何編碼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(response -> { ... }直接發送迴應的訂戶在登錄失敗的情況下。而不是

回答

1

.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 
    } 
})