即使在onError

时间:2016-05-24 07:54:30

标签: java rx-java retrofit2

当google +响应错误json

时,我总是得到一个未处理的异常
retrofit2.HttpException: HTTP 404 
                         at retrofit2.RxJavaCallAdapterFactory$SimpleCallAdapter$1.call(RxJavaCallAdapterFactory.java:159)
                         at retrofit2.RxJavaCallAdapterFactory$SimpleCallAdapter$1.call(RxJavaCallAdapterFactory.java:154)
                         at rx.internal.operators.OperatorMap$1.onNext(OperatorMap.java:54)
                         at retrofit2.RxJavaCallAdapterFactory$CallOnSubscribe.call(RxJavaCallAdapterFactory.java:109)
                         at retrofit2.RxJavaCallAdapterFactory$CallOnSubscribe.call(RxJavaCallAdapterFactory.java:88)
                         at rx.Observable$2.call(Observable.java:162)
                         at rx.Observable$2.call(Observable.java:154)
                         at rx.Observable$2.call(Observable.java:162)
                         at rx.Observable$2.call(Observable.java:154)
                         ....

在那段代码中:

Observable.create(new Observable.OnSubscribe<String>() {
        @Override
        public void call(Subscriber<? super String> strSub) {
            // Getting ID
            strSub.onNext(AccountUtils.getAccountId(appContext));
            strSub.onCompleted();})

          .subscribeOn(Schedulers.io())
          // Get Google+ Image through Retrofit2
          .flatMap(str -> createGPlusUserObservable(str, AccountUtils.ANDROID_API_KEY))
          .map(this::setprofileImage)    // I don't see Timber.d message inside that method!
          .compose(binder)
          .observeOn(AndroidSchedulers.mainThread())
          .subscribe(subscriber);

在createGPlusUserObservable中我使用Retrofit 2来获取谷歌+图像

private Observable<GPlusUser> createGPlusUserObservable(String userId, String apiKey) {
    //try {
    GoogleApiService service = ServiceFactory.getInstance().createJsonRetrofitService(
            GoogleApiService.class,
            GoogleApiService.SERVICE_ENDPOINT
    );

    Observable<GPlusUser> result = service.getGPlusUserInfo(userId, apiKey);
    Timber.d("Here1!"); // I see that in console!
    return result;  // It always returns result!
    /*} catch (Throwable e) { - it doesn't catch anything!
        Timber.d("Here!");
    }*/
}

订阅者是:

new Subscriber<GPlusUser>() {
        @Override
        public void onCompleted() {
            Timber.d("GPlusUserSubscriber ON COMPLETED");
        }

        @Override
        public void onError(Throwable e) {
            if (e instanceof HttpException) {
                Timber.d("RETROFIT!"); // I see that in console!
            }
        }

        @Override
        public void onNext(GPlusUser gPlusUser) {
            setupAccountBox();
        }
    };

更新:setprofileImage方法

private GPlusUser setprofileImage(GPlusUser gPlusUser) {
    Timber.d("FOUR"); // As I've said, it doesn't appear in console
    AccountUtils.setProfileImage(appContext, gPlusUser.image.url);
    Timber.d("Setting profile image: %s", gPlusUser.image.url);
    return gPlusUser;
}

所以问题是 - 如果我在订阅者的onError(Throwable e)中处理它,为什么我会得到未处理的异常

谢谢!

1 个答案:

答案 0 :(得分:0)

我认为这是因为在从纯html字符串转换为我的GPlusUser类时,改造工厂逻辑中会发生错误。

我已经通过Observable<Response<ResponseBody>> response使用纯HTML来消除控制台日志中的恼人异常及其response.isSuccess()