janishar / android-mvp-architecture

This repository contains a detailed sample app that implements MVP architecture using Dagger2, GreenDao, RxJava2, FastAndroidNetworking and PlaceholderView
https://janisharali.com
Apache License 2.0
4.43k stars 1.24k forks source link

Observer issues #60

Closed amitkumarverma-android closed 6 years ago

amitkumarverma-android commented 6 years ago

Hi, I have make server call after another server api call it return exception on second api response in same presenter.

Message-java.lang.NullPointerException: value == null

value == null

And My Code is

@Override public void onServerLoginClick(String email, String password) { //validate email and password if (TextUtils.isEmpty(email)) { getMvpView().onError(R.string.empty_email); return; } if (!CommonUtils.isEmailValid(email)) { getMvpView().onError(R.string.invalid_email); return; } if (TextUtils.isEmpty(password)) { getMvpView().onError(R.string.empty_password); return; } getMvpView().showLoading();

    getDataManager().doServerLoginApiCall(email, "password", password)
            .subscribeOn(getSchedulerProvider().io())
            .observeOn(getSchedulerProvider().ui())
            .subscribe(new Consumer<LoginResponse>() {
                @Override
                public void accept(LoginResponse response) throws Exception {
                    if (response.getError() == null) {
                        getDataManager().updateUserCredential(
                                response.getAccessToken(),
                                DataManager.LoggedInMode.LOGGED_IN_MODE_LOGGED_OUT,
                                response.getRefreshToken(),
                                response.getTokenType());

                        **fetchUserDetails();**

                    } else {
                        getMvpView().onError(response.getError());
                    }

                    getMvpView().hideLoading();

                }
            }, new Consumer<Throwable>() {
                @Override
                public void accept(Throwable throwable) throws Exception {
                    getMvpView().hideLoading();
                    // handle the login error here
                    handleApiError(throwable);

                }
            });
}

After this call I call metthod fetchUserDetails()

@Override public void fetchUserDetails() { getDataManager().doUserDetailsApiCall() .subscribeOn(getSchedulerProvider().io()) .observeOn(getSchedulerProvider().ui()) .subscribe(new Consumer() { @Override public void accept(UserDetailResponse response) throws Exception {

                    getMvpView().hideLoading();
                    getMvpView().openMainActivity();

                }
            }, new Consumer<Throwable>() {
                @Override
                public void accept(Throwable throwable) throws Exception {
                    getMvpView().hideLoading();   **//Throw Exception here**
                    // handle the login error here
                    handleApiError(throwable);

                }
            });
}
d-u-a-l commented 6 years ago

My guess is that's because of wrong realization of AppApiHelper. It is created before first call is made while you don't have any credentials data, but it requires ApiHeader as a constructor parameter, which in it's turn requires both PublicApiHeader and ProtectedApiHeader. Since both of them are created before login call and both are singletons, ProtectedApiHeader lacks of credentials data, so second request will fail