【问题标题】:retrofit2.HttpException thrown even though it was handled in onError即使在 onError 中处理了 retrofit2.HttpException 也会抛出
【发布时间】:2016-07-01 19:57:23
【问题描述】:

当 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 获取 google+ 图片

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) 中处理它,为什么会出现未处理的异常

谢谢!

【问题讨论】:

  • 你能贴出'setprofileImage'的代码吗?
  • 查看更新。谢谢!

标签: java rx-java retrofit2


【解决方案1】:

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

我通过Observable&lt;Response&lt;ResponseBody&gt;&gt; response 使用纯 html 消除了控制台日志中那个烦人的异常,它是 response.isSuccess()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-08-16
    • 2013-02-06
    • 1970-01-01
    • 2016-01-07
    • 1970-01-01
    • 1970-01-01
    • 2019-03-08
    相关资源
    最近更新 更多