2

当我从服务器请求数据时,几乎情况下 rxjava 可以onError()在我的 rxjava 链中从 okhttp3 捕获超时异常,但有时onError()无法处理超时并且我的应用程序崩溃并出现以下异常:

02-05 06:27:35.432 15865-15865/com.app.example E/AndroidRuntime: FATAL EXCEPTION: main
                                                                   Process: com.app.example, PID: 15865
                                                                   io.reactivex.exceptions.CompositeException: 2 exceptions occurred. 
                                                                       at io.reactivex.internal.observers.LambdaObserver.onError(LambdaObserver.java:80)
                                                                       at io.reactivex.internal.observers.BasicFuseableObserver.onError(BasicFuseableObserver.java:100)
                                                                       at io.reactivex.internal.operators.observable.ObservableDoOnEach$DoOnEachObserver.onError(ObservableDoOnEach.java:119)
                                                                       at io.reactivex.internal.observers.BasicFuseableObserver.onError(BasicFuseableObserver.java:100)
                                                                       at io.reactivex.internal.observers.BasicFuseableObserver.onError(BasicFuseableObserver.java:100)
                                                                       at io.reactivex.internal.operators.observable.ObservableDoOnEach$DoOnEachObserver.onError(ObservableDoOnEach.java:119)
                                                                       at io.reactivex.internal.operators.observable.ObservableDoOnEach$DoOnEachObserver.onError(ObservableDoOnEach.java:119)
                                                                       at io.reactivex.internal.operators.observable.ObservableDoOnEach$DoOnEachObserver.onError(ObservableDoOnEach.java:119)
                                                                       at io.reactivex.internal.operators.observable.ObservableObserveOn$ObserveOnObserver.checkTerminated(ObservableObserveOn.java:276)
                                                                       at io.reactivex.internal.operators.observable.ObservableObserveOn$ObserveOnObserver.drainNormal(ObservableObserveOn.java:172)
                                                                       at io.reactivex.internal.operators.observable.ObservableObserveOn$ObserveOnObserver.run(ObservableObserveOn.java:252)
                                                                       at io.reactivex.android.schedulers.HandlerScheduler$ScheduledRunnable.run(HandlerScheduler.java:109)
                                                                       at android.os.Handler.handleCallback(Handler.java:742)
                                                                       at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                       at android.os.Looper.loop(Looper.java:157)
                                                                       at android.app.ActivityThread.main(ActivityThread.java:5571)
                                                                       at java.lang.reflect.Method.invoke(Native Method)
                                                                       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:745)
                                                                       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:635)
                                                                     ComposedException 1 :
                                                                    java.net.SocketTimeoutException
                                                                       at java.net.PlainSocketImpl.read(PlainSocketImpl.java:484)
                                                                       at java.net.PlainSocketImpl.access$000(PlainSocketImpl.java:37)
                                                                       at java.net.PlainSocketImpl$PlainSocketInputStream.read(PlainSocketImpl.java:237)
                                                                       at okio.Okio$2.read(Okio.java:139)
                                                                       at okio.AsyncTimeout$2.read(AsyncTimeout.java:237)
                                                                       at okio.RealBufferedSource.indexOf(RealBufferedSource.java:345)
                                                                       at okio.RealBufferedSource.readUtf8LineStrict(RealBufferedSource.java:217)
                                                                       at okhttp3.internal.http1.Http1Codec.readHeaderLine(Http1Codec.java:212)
                                                                       at okhttp3.internal.http1.Http1Codec.readResponseHeaders(Http1Codec.java:189)
                                                                       at okhttp3.internal.http.CallServerInterceptor.intercept(CallServerInterceptor.java:88)
                                                                       at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147)
                                                                       at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:121)
                                                                       at com.facebook.stetho.okhttp3.StethoInterceptor.intercept(StethoInterceptor.java:56)
                                                                       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:125)
                                                                       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

有一些帖子建议我需要设置readTimeout()writeTimeout()但它并不能完全解决问题。这是我对 okhttp3 的配置:

@Provides
@Singleton
internal fun provideOkHttpClient(): OkHttpClient {
    return OkHttpClient.Builder()
        .connectTimeout(15, TimeUnit.SECONDS)
        .readTimeout(15, TimeUnit.SECONDS)
        .writeTimeout(15, TimeUnit.SECONDS)
        .addNetworkInterceptor(StethoInterceptor())
        .build()
}

和改造:

@Provides
@Singleton
internal fun provideRetrofit(client: OkHttpClient): Retrofit {
    return Retrofit.Builder()
        .baseUrl("http://abc.xyz/")
        .client(client)
        .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
        .addConverterFactory(SimpleXmlConverterFactory.create())
        .build()
}

这是我的相关库:

io.reactivex.rxjava2:rxjava:2.1.8

io.reactivex.rxjava2:rxandroid:2.0.1

com.squareup.okhttp3:okhttp:3.9.1

com.squareup.retrofit2:retrofit:2.3.0

com.squareup.retrofit2:converter-simplexml:2.3.0

com.squareup.retrofit2:adapter-rxjava2:2.3.0

com.facebook.stetho:stetho:1.5.0

com.facebook.stetho:stetho-okhttp3:1.5.0

4

0 回答 0