2017-07-07 1 views
0

Je suis nouveau à RxJava. J'ai un scénario où je veux appeler le premier service Web de connexion (getLoginObservable) et en cas de succès, je veux appeler un autre service Web (getFetchDataObservable) pour obtenir l'information d'utilisateur.RxAndroid appel un appel réseau après l'autre

J'ai le code suivant qui fonctionne si la connexion est réussie. Mais je suis incapable de comprendre comment coder failure cas.

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

Comme je le sais RxJava, je peux comprendre que getLoginObservable(editTextUsername.getText().toString(), password) réponse d'envoi observable à la carte (map(response -> { ... }) et cette réponse carte de retour à flatmap (flatMap(response -> { ... }) et sa réponse est envoyée à l'abonné. Ici, je suis juste perdu que comment puis-je sauter (deuxième appel réseau) flatmap flatMap(response -> { ... } pour envoyer une réponse directement à l'abonné en cas d'échec de connexion.

Répondre

1

au lieu de:

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

vous pouvez utiliser:

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