Observable.empty() 导致 java.util.NoSuchElementException: 序列不包含任何元素

Observable.empty() causes java.util.NoSuchElementException: Sequence contains no elements

我正在使用 Retrofit 2.0.0-beta2 和 RxJava 1.0.14。我以这种方式处理错误,因为我需要在 doFinally:

中执行一些代码

.onErrorResumeNext(Observable.empty());

但是当我收到带有错误的 http 响应(例如 401)时,我的应用程序崩溃了,堆栈跟踪中没有我的 类。如果使用 Observable.never 不会有什么不好的事情发生。这是完整的堆栈跟踪:

java.lang.IllegalStateException: Exception thrown on Scheduler.Worker thread. Add `onError` handling.
at rx.internal.schedulers.ScheduledAction.run (ScheduledAction.java:60)
at android.os.Handler.handleCallback (Handler.java:739)
at android.os.Handler.dispatchMessage (Handler.java:95)
at android.os.Looper.loop (Looper.java:135)
at android.app.ActivityThread.main (ActivityThread.java:5221)
at java.lang.reflect.Method.invoke (Unknown source)
at java.lang.reflect.Method.invoke (Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run (ZygoteInit.java:899)
at com.android.internal.os.ZygoteInit.main (ZygoteInit.java:694)

rx.exceptions.OnErrorNotImplementedException: Sequence contains no elements
at rx.Observable.onError (Observable.java:7535)
at rx.observers.SafeSubscriber._onError (SafeSubscriber.java:154)
at rx.observers.SafeSubscriber.onError (SafeSubscriber.java:111)
at rx.internal.operators.OperatorDoOnEach.onError (OperatorDoOnEach.java:70)
at rx.internal.operators.OperatorDoOnEach.onError (OperatorDoOnEach.java:70)
at rx.internal.operators.OperatorDoOnEach.onError (OperatorDoOnEach.java:70)
at rx.internal.operators.OperatorObserveOn$ObserveOnSubscriber.pollQueue (OperatorObserveOn.java:197)
at rx.internal.operators.OperatorObserveOn$ObserveOnSubscriber.call (OperatorObserveOn.java:170)
at rx.internal.schedulers.ScheduledAction.run (ScheduledAction.java:55)
at android.os.Handler.handleCallback (Handler.java:739)
at android.os.Handler.dispatchMessage (Handler.java:95)
at android.os.Looper.loop (Looper.java:135)
at android.app.ActivityThread.main (ActivityThread.java:5221)
at java.lang.reflect.Method.invoke (Unknown source)
at java.lang.reflect.Method.invoke (Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run (ZygoteInit.java:899)
at com.android.internal.os.ZygoteInit.main (ZygoteInit.java:694)

java.util.NoSuchElementException: Sequence contains no elements
at rx.internal.operators.OperatorSingle$ParentSubscriber.onCompleted (OperatorSingle.java:131)
at rx.internal.operators.OperatorTake.onCompleted (OperatorTake.java:53)
at rx.Observable$EmptyHolder.call (Observable.java:1077)
at rx.Observable$EmptyHolder.call (Observable.java:1074)
at rx.Observable.unsafeSubscribe (Observable.java:7710)
at rx.internal.operators.OperatorOnErrorResumeNextViaObservable.onError (OperatorOnErrorResumeNextViaObservable.java:76)
at rx.internal.operators.OperatorDoOnEach.onError (OperatorDoOnEach.java:70)
at rx.internal.operators.OperatorSubscribeOn.onError (OperatorSubscribeOn.java:71)
at rx.observers.SerializedObserver.onError (SerializedObserver.java:159)
at rx.observers.SerializedSubscriber.onError (SerializedSubscriber.java:79)
at rx.internal.operators.OperatorTakeUntil.onError (OperatorTakeUntil.java:49)
at rx.internal.operators.OperatorMerge$MergeSubscriber.reportError (OperatorMerge.java:239)
at rx.internal.operators.OperatorMerge$MergeSubscriber.checkTerminate (OperatorMerge.java:774)
at rx.internal.operators.OperatorMerge$MergeSubscriber.emitLoop (OperatorMerge.java:532)
at rx.internal.operators.OperatorMerge$MergeSubscriber.emit (OperatorMerge.java:521)
at rx.internal.operators.OperatorMerge$InnerSubscriber.onError (OperatorMerge.java:808)
at rx.Observable$ThrowObservable.call (Observable.java:9600)
at rx.Observable$ThrowObservable.call (Observable.java:9590)
at rx.Observable.unsafeSubscribe (Observable.java:7710)
at rx.internal.operators.OperatorMerge$MergeSubscriber.onNext (OperatorMerge.java:231)
at rx.internal.operators.OperatorMerge$MergeSubscriber.onNext (OperatorMerge.java:140)
at rx.internal.operators.OperatorMap.onNext (OperatorMap.java:55)
at retrofit.RxJavaCallAdapterFactory$CallOnSubscribe.call (RxJavaCallAdapterFactory.java:113)
at retrofit.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)
at rx.Observable.call (Observable.java:162)
at rx.Observable.call (Observable.java:154)
at rx.Observable.unsafeSubscribe (Observable.java:7710)
at rx.internal.operators.OperatorSubscribeOn.call (OperatorSubscribeOn.java:62)
at rx.internal.schedulers.ScheduledAction.run (ScheduledAction.java:55)
at java.util.concurrent.Executors$RunnableAdapter.call (Executors.java:422)
at java.util.concurrent.FutureTask.run (FutureTask.java:237)
at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access1 (ScheduledThreadPoolExecutor.java:152)
at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run (ScheduledThreadPoolExecutor.java:265)
at java.util.concurrent.ThreadPoolExecutor.runWorker (ThreadPoolExecutor.java:1112)
at java.util.concurrent.ThreadPoolExecutor$Worker.run (ThreadPoolExecutor.java:587)
at java.lang.Thread.run (Thread.java:818)

谢谢,
安东

从堆栈跟踪,

at rx.internal.operators.OperatorSingle$ParentSubscriber.onCompleted (OperatorSingle.java:131)
at rx.internal.operators.OperatorTake.onCompleted (OperatorTake.java:53)

这等同于 take(1).single(),这也是 first() 便利运算符。我怀疑您使用的是后者,但两者都至少需要一个元素。

通过使用 empty(),您正在创建一个不包含违反下游运营商合同的元素的可观察对象。要么在 onErrorResumeNext 可观察对象中发出一个默认项,要么更改您的下游代码以支持没有元素的流。