Observable.create(new ObservableOnSubscribe())
.subscribeOn(Schedulers.io())
.compose(new ParserTransformer())
.map()
.subscribe()
当我的ObservableOnSubscribe出现问题时,会抛出异常(java.net.UnknownHostException ...)。
ObservableOnSubscribe:
public void subscribe(ObservableEmitter oe) throws Exception {
try{
...
} catch {
if (!oe.isDisposed()) {
if (ex instanceof IOException) {
throw new NetException(...); //sometimes, it make the app crash!
}
} else{
...
}
}
oe.onComplete();
}
这是日志:
D OkHttp: error: java.net.UnknownHostException
D OkHttp: oe.isDisposed: false
W System.err: io.reactivex.exceptions.OnErrorNotImplementedException: Unable to resolve host ******: No address associated with hostname
W System.err: at io.reactivex.internal.functions.Functions$OnErrorMissingConsumer.accept(Functions.java:704)
W System.err: at io.reactivex.internal.functions.Functions$OnErrorMissingConsumer.accept(Functions.java:701)
W System.err: at io.reactivex.internal.observers.LambdaObserver.onError(LambdaObserver.java:77)
W System.err: at io.reactivex.internal.observers.BasicFuseableObserver.onError(BasicFuseableObserver.java:100)
W System.err: at io.reactivex.internal.operators.observable.ObservableObserveOn$ObserveOnObserver.checkTerminated(ObservableObserveOn.java:276)
W System.err: at io.reactivex.internal.operators.observable.ObservableObserveOn$ObserveOnObserver.drainNormal(ObservableObserveOn.java:172)
W System.err: at io.reactivex.internal.operators.observable.ObservableObserveOn$ObserveOnObserver.run(ObservableObserveOn.java:252)
W System.err: at io.reactivex.android.schedulers.HandlerScheduler$ScheduledRunnable.run(HandlerScheduler.java:109)
W System.err: at android.os.Handler.handleCallback(Handler.java:754)
W System.err: at android.os.Handler.dispatchMessage(Handler.java:95)
W System.err: at android.os.Looper.loop(Looper.java:163)
W System.err: at android.app.ActivityThread.main(ActivityThread.java:6343)
W System.err: at java.lang.reflect.Method.invoke(Native Method)
W System.err: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:880)
W System.err: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:770)
为什么会这样?为什么rxjava说没有实现OnError?
答案 0 :(得分:3)
您应该使用Emitter.onError
而不是抛出异常。
编辑:请记住javadocs:OnErrorNotImplementedException
public void subscribe(ObservableEmitter oe) throws Exception {
try{
...
} catch {
if (!oe.isDisposed()) {
if (ex instanceof IOException) {
oe.onError(new NetException(...)); //pass error to emitter
}
} else{
...
}
}
oe.onComplete();
}
答案 1 :(得分:2)
发生错误,您未在订阅者中实现onError()方法。
Observable.create(new ObservableOnSubscribe())
.subscribeOn(Schedulers.io())
.compose(new ParserTransformer())
.map()
.subscribe(new Consumer<Object>() {
@Override
public void accept(Object object) {
}
}, new Consumer<Throwable>() {
@Override
public void accept(Throwable throwable) {
Log.e(TAG, throwable.getMessage(), throwable);
}
});)