retrofit2.HttpException 尽管它是在 onError 中处理的,但仍被抛出
retrofit2.HttpException thrown even though it was handled in onError
当 google+ 响应错误 json
时,我总是遇到未处理的异常
retrofit2.HttpException: HTTP 404
at retrofit2.RxJavaCallAdapterFactory$SimpleCallAdapter.call(RxJavaCallAdapterFactory.java:159)
at retrofit2.RxJavaCallAdapterFactory$SimpleCallAdapter.call(RxJavaCallAdapterFactory.java:154)
at rx.internal.operators.OperatorMap.onNext(OperatorMap.java:54)
at retrofit2.RxJavaCallAdapterFactory$CallOnSubscribe.call(RxJavaCallAdapterFactory.java:109)
at retrofit2.RxJavaCallAdapterFactory$CallOnSubscribe.call(RxJavaCallAdapterFactory.java:88)
at rx.Observable.call(Observable.java:162)
at rx.Observable.call(Observable.java:154)
at rx.Observable.call(Observable.java:162)
at rx.Observable.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) 中处理它,为什么会出现未处理的异常
谢谢!
我认为这是因为改造工厂逻辑发生错误,同时从纯 html 字符串转换为我的 GPlusUser class.
我通过 Observable<Response<ResponseBody>> response
使用纯 html 消除了控制台日志中那个恼人的异常,它是 response.isSuccess()
当 google+ 响应错误 json
时,我总是遇到未处理的异常retrofit2.HttpException: HTTP 404
at retrofit2.RxJavaCallAdapterFactory$SimpleCallAdapter.call(RxJavaCallAdapterFactory.java:159)
at retrofit2.RxJavaCallAdapterFactory$SimpleCallAdapter.call(RxJavaCallAdapterFactory.java:154)
at rx.internal.operators.OperatorMap.onNext(OperatorMap.java:54)
at retrofit2.RxJavaCallAdapterFactory$CallOnSubscribe.call(RxJavaCallAdapterFactory.java:109)
at retrofit2.RxJavaCallAdapterFactory$CallOnSubscribe.call(RxJavaCallAdapterFactory.java:88)
at rx.Observable.call(Observable.java:162)
at rx.Observable.call(Observable.java:154)
at rx.Observable.call(Observable.java:162)
at rx.Observable.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) 中处理它,为什么会出现未处理的异常
谢谢!
我认为这是因为改造工厂逻辑发生错误,同时从纯 html 字符串转换为我的 GPlusUser class.
我通过 Observable<Response<ResponseBody>> response
使用纯 html 消除了控制台日志中那个恼人的异常,它是 response.isSuccess()