首页 文章

SocketTimeoutException由RxJava未处理

提问于
浏览
0

我有一个使用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)

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

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

1 回答

  • 0

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

    • retrofit sourcecode之后,可能存在一个竞争条件,其中 Disposable.dispose() 被调用但超时发生在基础 Call 被取消之前 . 这将导致超时被发送到观察者之后_1120298_,从而触发崩溃 .

    • onError 处理程序内部抛出错误 . 这不应该根据RxJava协议发生,并且会导致 CompositeException 崩溃 . 由于您发布的错误不包括确切的Exception类,因此不能排除这一点 .

相关问题