SocketTimeoutException由RxJava未处理

时间:2017-10-19 10:42:38

标签: java android retrofit2 okhttp rx-java2

我有一个使用sdk 25版开发的Android项目。我使用RXJava作为我的线程管理和Retrofit库来打网络。

我还实现了自定义拦截器来添加覆盖有趣的拦截(链:Interceptor.Chain):响应? {

    val request = addHeader(chain)

    val response = chain.proceed(request)
    checkErrorResponse(response)

    return response

api调用将始终在RX Java流程中,我正在确定它。将我的APK放到Playstore后,Crashlytics会检测到崩溃。

#0. Crashed: main: 0 0 0x0000000000000000
   at okio.Okio$4.newTimeoutException(Okio.java:230)
   at okio.AsyncTimeout.exit(AsyncTimeout.java:285)
   at okio.AsyncTimeout$2.read(AsyncTimeout.java:241)
   at okio.RealBufferedSource.indexOf(RealBufferedSource.java:345)
   at okio.RealBufferedSource.readUtf8LineStrict(RealBufferedSource.java:217)
   at okio.RealBufferedSource.readUtf8LineStrict(RealBufferedSource.java:211)
   at okhttp3.internal.http1.Http1Codec.readResponseHeaders(Http1Codec.java:189)
   at okhttp3.internal.http.CallServerInterceptor.intercept(CallServerInterceptor.java:75)
   at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:92)
   at okhttp3.internal.connection.ConnectInterceptor.intercept(ConnectInterceptor.java:45)
   at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:92)
   at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:67)
   at okhttp3.internal.cache.CacheInterceptor.intercept(CacheInterceptor.java:93)
   at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:92)
   at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:67)
   at okhttp3.internal.http.BridgeInterceptor.intercept(BridgeInterceptor.java:93)
   at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:92)
   at okhttp3.internal.http.RetryAndFollowUpInterceptor.intercept(RetryAndFollowUpInterceptor.java:120)
   at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:92)
   at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:67)
   at com.payfazz.data.base.net.PayfazzInterceptor.intercept(PayfazzInterceptor.kt:24)
   at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:92)
   at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:67)
   at okhttp3.RealCall.getResponseWithInterceptorChain(RealCall.java:185)
   at okhttp3.RealCall.execute(RealCall.java:69)
   at retrofit2.OkHttpCall.execute(OkHttpCall.java:180)
   at retrofit2.adapter.rxjava2.CallExecuteObservable.subscribeActual(CallExecuteObservable.java:41)
   at io.reactivex.Observable.subscribe(Observable.java:10842)
   at io.reactivex.internal.operators.observable.ObservableMap.subscribeActual(ObservableMap.java:33)
   at io.reactivex.Observable.subscribe(Observable.java:10842)
   at io.reactivex.internal.operators.observable.ObservableMap.subscribeActual(ObservableMap.java:33)
   at io.reactivex.Observable.subscribe(Observable.java:10842)
   at io.reactivex.internal.operators.observable.ObservableDefer.subscribeActual(ObservableDefer.java:39)
   at io.reactivex.Observable.subscribe(Observable.java:10842)
   at io.reactivex.internal.operators.observable.ObservableDefer.subscribeActual(ObservableDefer.java:39)
   at io.reactivex.Observable.subscribe(Observable.java:10842)
   at io.reactivex.internal.operators.observable.ObservableSubscribeOn$SubscribeTask.run(ObservableSubscribeOn.java:96)
   at io.reactivex.internal.schedulers.ExecutorScheduler$ExecutorWorker$BooleanRunnable.run(ExecutorScheduler.java:260)
   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)

当我研究堆栈跟踪时,我发现我的代码正好在我的自定义拦截器上。据我所知,崩溃是由SocketTimeoutException引起的,然后是超时请求。然后我尝试重现超时但无法完成。在我的环境中,异常总是由RX Java捕获并发送到onError()方法。

RX Java怎么没有抓住崩溃?为了安全起见,我应该使用catch块包装proceed()方法吗?

1 个答案:

答案 0 :(得分:0)

当在RxJava生命周期之外抛出异常时,会导致这类崩溃。这意味着您的例外可能由以下任一选项引起:

  1. retrofit sourcecode之后,可能存在竞争条件,其中Disposable.dispose()被调用,但超时发生在基础Call被取消之前。这会导致超时在disposed后发送给观察者,从而触发崩溃。
  2. onError处理程序内部抛出错误。这不应该根据RxJava协议发生,并且会导致CompositeException崩溃。由于您发布的错误不包括确切的Exception类,因此不能排除这一点。