0
我已經找到了一些解決方案(OnErrorNotImplementedException when Interceptor throws.),但我仍然得到OnErrorNotImplementedException
和SocketTimeoutException
和應用程序崩潰得到墜毀。我用rxjava2 + retrofit2 + okhttp3在我的Android應用程序,當應用程序沒有連接到互聯網
我的代碼來處理HTTP結果:
observable.compose(TransformerHelper.<Response<T>>io_main()).doOnSubscribe(new Consumer<Disposable>() {
@Override
public void accept(@io.reactivex.annotations.NonNull Disposable disposable) throws Exception {
requestCallback.onBefore();
}
}).doOnError(new Consumer<Throwable>() {
@Override
public void accept(@io.reactivex.annotations.NonNull Throwable throwable) throws Exception {
requestCallback.onAfter();
requestCallback.onError(throwable);
}
}).doOnComplete(new Action() {
@Override
public void run() throws Exception {
requestCallback.onAfter();
}
}).onErrorReturn(new Function<Throwable, Response<T>>() {
@Override
public Response<T> apply(@io.reactivex.annotations.NonNull Throwable throwable) throws Exception {
return null;
}
}).subscribe(new Consumer<Response<T>>() {
@Override
public void accept(@io.reactivex.annotations.NonNull Response<T> response) throws Exception {
try {
requestCallback.onAfter();
if (response.isSuccess()) {
requestCallback.onSuccess(response.data, response.msg);
} else {
requestCallback.onBizErr(response.code, response.msg);
}
} catch (Exception e) {
requestCallback.onAfter();
requestCallback.onError(e.getCause());
}
}
}, new Consumer<Throwable>() {
@Override
public void accept(@io.reactivex.annotations.NonNull Throwable throwable) throws Exception {
requestCallback.onAfter();
requestCallback.onError(throwable);
}
});
如果您想增加獲得答案的機會,您應該將其粘貼到堆棧跟蹤中。另外,爲什麼你在'onErrorReturn'中返回null。 'null'沒有isSuccess方法。 – Lukasz