Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/339.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
获取数据时发生方向更改时发生RxJava2 UndeliverableException_Java_Android_Kotlin_Rx Java2 - Fatal编程技术网

获取数据时发生方向更改时发生RxJava2 UndeliverableException

获取数据时发生方向更改时发生RxJava2 UndeliverableException,java,android,kotlin,rx-java2,Java,Android,Kotlin,Rx Java2,如果在我的应用程序获取新的redditNews时更改了设备的方向,则会出现以下错误 E/AndroidRuntime: FATAL EXCEPTION: RxCachedThreadScheduler-1 Process: com.spicywdev.schmeddit, PID: 26522 io.reactivex.exceptions.UndeliverableException: The exception could not be delivered to the

如果在我的应用程序获取新的redditNews时更改了设备的方向,则会出现以下错误

  E/AndroidRuntime: FATAL EXCEPTION: RxCachedThreadScheduler-1
    Process: com.spicywdev.schmeddit, PID: 26522
    io.reactivex.exceptions.UndeliverableException: The exception could not be delivered to the consumer because it has already canceled/disposed the flow or the exception has nowhere to go to begin with. Further reading: https://github.com/ReactiveX/RxJava/wiki/What's-different-in-2.0#error-handling | null
        at io.reactivex.plugins.RxJavaPlugins.onError(RxJavaPlugins.java:367)
        at io.reactivex.internal.operators.observable.ObservableCreate$CreateEmitter.onError(ObservableCreate.java:73)
        at io.reactivex.internal.operators.observable.ObservableCreate.subscribeActual(ObservableCreate.java:43)
        at io.reactivex.Observable.subscribe(Observable.java:12090)
        at io.reactivex.internal.operators.observable.ObservableSubscribeOn$SubscribeTask.run(ObservableSubscribeOn.java:96)
        at io.reactivex.Scheduler$DisposeTask.run(Scheduler.java:578)
        at io.reactivex.internal.schedulers.ScheduledRunnable.run(ScheduledRunnable.java:66)
        at io.reactivex.internal.schedulers.ScheduledRunnable.call(ScheduledRunnable.java:57)
        at java.util.concurrent.FutureTask.run(FutureTask.java:237)
        at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:272)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1133)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:607)
        at java.lang.Thread.run(Thread.java:762)
     Caused by: java.io.InterruptedIOException
        at okhttp3.internal.http2.Http2Stream.waitForIo(Http2Stream.java:579)
        at okhttp3.internal.http2.Http2Stream.takeResponseHeaders(Http2Stream.java:143)
        at okhttp3.internal.http2.Http2Codec.readResponseHeaders(Http2Codec.java:125)
        at okhttp3.internal.http.CallServerInterceptor.intercept(CallServerInterceptor.java:88)
        at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147)
        at okhttp3.internal.connection.ConnectInterceptor.intercept(ConnectInterceptor.java:45)
        at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147)
        at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:121)
        at okhttp3.internal.cache.CacheInterceptor.intercept(CacheInterceptor.java:93)
        at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147)
        at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:121)
        at okhttp3.internal.http.BridgeInterceptor.intercept(BridgeInterceptor.java:93)
        at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147)
        at okhttp3.internal.http.RetryAndFollowUpInterceptor.intercept(RetryAndFollowUpInterceptor.java:126)
        at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147)
        at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:121)
        at okhttp3.RealCall.getResponseWithInterceptorChain(RealCall.java:200)
        at okhttp3.RealCall.execute(RealCall.java:77)
        at retrofit2.OkHttpCall.execute(OkHttpCall.java:180)
        at retrofit2.ExecutorCallAdapterFactory$ExecutorCallbackCall.execute(ExecutorCallAdapterFactory.java:91)
        at com.spicywdev.schmeddit.features.news.NewsManager$getNews$1.subscribe(NewsManager.kt:16)
        at io.reactivex.internal.operators.observable.ObservableCreate.subscribeActual(ObservableCreate.java:40)
这就是这门课的样子。函数
requestNews()
中发生错误


我对RxJava很陌生——如果有人能帮我解决这个问题,我会非常高兴。多谢各位

您可以通过以下方式覆盖Rx错误处理程序:

  • 使用自定义MyApplication扩展应用程序类
  • 然后在应用程序类的onCreate方法中编写:

    @Override
    public void onCreate() {
        super.onCreate();
        RxJavaPlugins.setErrorHandler(throwable -> {}); // nothing or some logging
    }
    
  • 将MyApplication类添加到清单:

    <application
        android:name=".MyApplication"
        ...>
    

    可能是(未测试),有可能用onNext(标记)改变可观察到的错误,标记使“正常onNext”和“错误onNext”(替换on错误)之间存在差异

    onNext(0)=>是由于异常引起的(您可以通过公共静态字符串获取异常)

    onNext(1)=>无错误

    发射器.onError(ex);=>发射器.onNext(0)

    在《观察家报》中:

    @Override
    public void onNext(Integer message) {
        if (message ==0 )
            handleError();
        else
            handleNext();
    }
    

    当然,它不是很“干净”,但它可以解决问题…

    为了捕获第三方库或您自己的代码可能会引发的此类错误,请使用RxJavaPlugins,并在有趣的情况下。。。如果您使用Rx写入此代码,则未锁定的Rx错误将传递到此代码,因此必须检查异常类型:

    RxJavaPlugins.setErrorHandler {
            if( it is  UndeliverableException){
                Toast.makeText(view?.getContext(), "UndeliverableException: " + it.message, Toast.LENGTH_LONG).show()
                return@setErrorHandler
            }
        }
    
    有关参考信息,请查看此链接:

    您是否阅读了异常消息?@akarnokd是的,我想我理解问题所在。异常未到达其目标。但我真的不明白如何在RxJava的上下文中修复它/错误消息中有一个指向要读取的wiki的链接。wiki部分中有一个这样的示例:搜索
    RxJavaPlugins.setErrorHandler
    。这是一个全局处理程序,在应用程序生命周期的早期某处定义它。使用Kotlin,它更简单
    RxJavaPlugins.setErrorHandler{}
    RxJavaPlugins.setErrorHandler {
            if( it is  UndeliverableException){
                Toast.makeText(view?.getContext(), "UndeliverableException: " + it.message, Toast.LENGTH_LONG).show()
                return@setErrorHandler
            }
        }