gpt4 book ai didi

java - Observable.empty() 导致 java.util.NoSuchElementException : Sequence contains no elements

转载 作者:太空狗 更新时间:2023-10-29 23:01:01 26 4
gpt4 key购买 nike

我正在使用 Retrofit 2.0.0-beta2 和 RxJava 1.0.14。我这样处理错误是因为我需要在 doFinally 中执行一些代码:

.onErrorResumeNext(Observable.empty());

但是,当我收到带有错误(例如 401)的 http 响应时,我的应用程序崩溃了,堆栈跟踪中没有我的类。如果使用 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$27.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$1.onError (OperatorDoOnEach.java:70)
at rx.internal.operators.OperatorDoOnEach$1.onError (OperatorDoOnEach.java:70)
at rx.internal.operators.OperatorDoOnEach$1.onError (OperatorDoOnEach.java:70)
at rx.internal.operators.OperatorObserveOn$ObserveOnSubscriber.pollQueue (OperatorObserveOn.java:197)
at rx.internal.operators.OperatorObserveOn$ObserveOnSubscriber$2.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$1.onCompleted (OperatorTake.java:53)
at rx.Observable$EmptyHolder$1.call (Observable.java:1077)
at rx.Observable$EmptyHolder$1.call (Observable.java:1074)
at rx.Observable.unsafeSubscribe (Observable.java:7710)
at rx.internal.operators.OperatorOnErrorResumeNextViaObservable$1.onError (OperatorOnErrorResumeNextViaObservable.java:76)
at rx.internal.operators.OperatorDoOnEach$1.onError (OperatorDoOnEach.java:70)
at rx.internal.operators.OperatorSubscribeOn$1$1$1.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$1.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$1.call (Observable.java:9600)
at rx.Observable$ThrowObservable$1.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$1.onNext (OperatorMap.java:55)
at retrofit.RxJavaCallAdapterFactory$CallOnSubscribe.call (RxJavaCallAdapterFactory.java:113)
at retrofit.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)
at rx.Observable$2.call (Observable.java:162)
at rx.Observable$2.call (Observable.java:154)
at rx.Observable.unsafeSubscribe (Observable.java:7710)
at rx.internal.operators.OperatorSubscribeOn$1$1.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.access$201 (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$1.onCompleted (OperatorTake.java:53)

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

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

关于java - Observable.empty() 导致 java.util.NoSuchElementException : Sequence contains no elements,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33005866/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com