dimanche 3 juillet 2016

Do something after login success using RxJava and Retrofit 2

I implemented the network call to validate the login, which is working fine. I am just facing some trouble doing another call to get user's data after the login is successful.

Here is the API call:

RestApi restApi = ServiceRest.createRetrofitService(RestApi.class, UrlServer.URL_SERVER);
Observable<Response<User>> responseObservable = restApi.getUser(user);
responseObservable.subscribeOn(Schedulers.io())
        .observeOn(AndroidSchedulers.mainThread())
        .unsubscribeOn(Schedulers.io())
        .subscribe(new Subscriber<Response<User>>() {
            @Override
            public void onCompleted() {
                Log.i("LoginActivity", "[onCompleted]");
                progressDialog.dismiss();
            }

            @Override
            public void onError(Throwable e) {
                progressDialog.dismiss();
                Toast.makeText(LoginActivity.this, "Error: " + e.getMessage(), Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onNext(Response<User> userResponse) {
                //Do stuff to go to another Activity
            }
        });

I want to, after the login is successful, call another url using the token obtained through the Response object User and send it to the other api call (Observable<Response<Object>> getData(@Header("Authorization") String token);) as an Authorization of the Header. I have tried using flatMap and zip, but I wasn't able to accomplish this task.
Any advices? This concept is new to me.

Aucun commentaire:

Enregistrer un commentaire