RxJava2:“onSuccess使用null调用。在2.x运算符中通常不允许空值”

时间:2018-01-05 22:33:36

标签: android rx-java2

我还在学习RxJava,所以我很难搞清楚这一点......

问题:

API成功,但在Single<ResponseRegistration> postRegisterAccount()内生成错误onSuccess called with null. Null values are generally not allowed in 2.x operators

代码:

// in the View Layer
addRxSubscription(mService.postRegistration()
        .subscribeOn(Schedulers.io())
        .observeOn(AndroidSchedulers.mainThread())
        .subscribe(success -> {
                    if (success != null && success) {
                        //successfully registered
                        updateNavigationDrawerMenu();
                    } else {
                        Log.e(TAG, "Failed to register !");
                    }
                }, throwable -> Log.e(TAG, "Failed to register !")
        ));
break;


// in the Service Layer, passes in the API parameters
public Single<Boolean> postRegistration() {
    final String email = "test@email.com";
    final String pass = "testpass";

    PostRegistrationData registrationData = new PostRegistrationData(email, pass);
    Single<ResponseRegistration> singlePostRegistration = mApi.postRegisterAccount(registrationData);

    return Single.create(subscriber -> singlePostRegistration.subscribeOn(Schedulers.io())
                .observeOn(Schedulers.io())
                .subscribe(registrationResponse -> {
                    if (registrationResponse.userId != null) {
                        // Success, store the response and let UI know API was successful
                        storeRegistrationId(registrationResponse.userId);
                        if (!subscriber.isDisposed()) {
                            subscriber.onSuccess(true);
                        }
                    }
                    else {
                        // Failure, let UI know API was un-successful
                        if (!subscriber.isDisposed()) {
                            subscriber.onSuccess(false);
                        }
                    }
                }, throwable -> {
                    // Failure
                    if (!subscriber.isDisposed()) {
                        //
                        // Due to the failure in postRegisterAccount(), this code executes
                        //
                        subscriber.onError(throwable);
                    }
                }));
}


// in the API layer - this strips the Response<ResponseGeneric<T>> wrapper from around the response data
public Single<ResponseRegistration> postRegisterAccount(PostRegistrationData registrationData) {
    Single<Response<ResponseGeneric<ResponseRegistration>>> postRegistration = apiClient.postRegisterAccount(registrationData);

    return Single.create(subscriber -> postRegistration.subscribeOn(Schedulers.io())
            .observeOn(Schedulers.io())
            .subscribe(response -> {
                if(response.isSuccessful() && response.body() != null) {
                    //
                    // this part is successful - the response data is received
                    // HOWEVER java.lang.NullPointerException: onSuccess called with null. Null values are generally not allowed in 2.x operators and sources.
                    // subscriber = null
                    //
                    if (!subscriber.isDisposed()) subscriber.onSuccess(response.body().data);
                }
            }, throwable -> {
                throwable.printStackTrace();
                if(!subscriber.isDisposed()) subscriber.onError(throwable);
            }));
}


// Retrofit Interface
@Headers({"Content-Type: application/json"})
@POST(ServicesConstants.API_PREFIX + "registration/")
Single<Response<ResponseGeneric<ResponseRegistration>>> postRegisterAccount(@Body PostRegistrationData postRegistrationData);

问题:

为什么subscriberpostRegisterAccount()为空?

0 个答案:

没有答案