SocketTimeoutException 使应用程序崩溃

Posted

技术标签:

【中文标题】SocketTimeoutException 使应用程序崩溃【英文标题】:SocketTimeoutException crashes the app 【发布时间】:2017-11-10 13:06:39 【问题描述】:

当我使用我的应用程序时遇到致命错误,我将手机设置为飞行模式以模拟没有互联网连接,当我打开带有互联网请求的片段时,我遇到了崩溃。请帮我解决这个问题。我不知道如何以及在哪里可以捕获或处理该异常。

感谢您的建议

代码运行的地方

override fun onViewCreated(view: View?, savedInstanceState: Bundle?) 
    super.onViewCreated(view, savedInstanceState)


    sharedSessionDataStorage.cardProvider
            .getCardProvider()
            .bindToLifecycle(this)
            .doFinally  stopProgress() 
            .subscribe(
                stopProgress()
                updateCardAdapter(it)
            , 
                stopProgress()
                Ln.e(it)
            )


我的依赖列表是

compile 'io.reactivex.rxjava2:rxandroid:2.0.1'
compile 'io.reactivex.rxjava2:rxjava:2.0.1'

compile 'com.squareup.retrofit2:retrofit:2.3.0'

compile 'com.squareup.retrofit2:adapter-rxjava2:2.3.0'
compile 'com.squareup.retrofit2:converter-gson:2.0.0'

compile 'com.squareup.okhttp3:okhttp:3.8.0'
compile 'com.squareup.okhttp3:logging-interceptor:3.8.0'

错误日志:

W/art: Suspending all threads took: 8.780ms
W/System.err: java.net.SocketTimeoutException: timeout
W/System.err:     at okio.Okio$4.newTimeoutException(Okio.java:230)
W/System.err:     at okio.AsyncTimeout.exit(AsyncTimeout.java:275)
W/System.err:     at okio.AsyncTimeout$2.read(AsyncTimeout.java:243)
W/System.err:     at okio.RealBufferedSource.indexOf(RealBufferedSource.java:345)
W/System.err:     at okio.RealBufferedSource.readUtf8LineStrict(RealBufferedSource.java:217)
W/System.err:     at okio.RealBufferedSource.readUtf8LineStrict(RealBufferedSource.java:211)
W/System.err:     at okhttp3.internal.http1.Http1Codec.readResponseHeaders(Http1Codec.java:189)
W/System.err:     at okhttp3.internal.http.CallServerInterceptor.intercept(CallServerInterceptor.java:75)
W/System.err:     at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:92)
W/System.err:     at okhttp3.internal.connection.ConnectInterceptor.intercept(ConnectInterceptor.java:45)
W/System.err:     at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:92)
W/System.err:     at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:67)
W/System.err:     at okhttp3.internal.cache.CacheInterceptor.intercept(CacheInterceptor.java:93)
W/System.err:     at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:92)
W/System.err:     at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:67)
W/System.err:     at okhttp3.internal.http.BridgeInterceptor.intercept(BridgeInterceptor.java:93)
W/System.err:     at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:92)
W/System.err:     at okhttp3.internal.http.RetryAndFollowUpInterceptor.intercept(RetryAndFollowUpInterceptor.java:120)
W/System.err:     at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:92)
W/System.err:     at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:67)
W/System.err:     at com.ihsanbal.logging.LoggingInterceptor.intercept(LoggingInterceptor.java:56)
W/System.err:     at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:92)
W/System.err:     at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:67)
W/System.err:     at com.readystatesoftware.chuck.ChuckInterceptor.intercept(ChuckInterceptor.java:172)
W/System.err:     at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:92)
W/System.err:     at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:67)
W/System.err:     at okhttp3.RealCall.getResponseWithInterceptorChain(RealCall.java:185)
W/System.err:     at okhttp3.RealCall.execute(RealCall.java:69)
W/System.err:     at retrofit2.OkHttpCall.execute(OkHttpCall.java:180)
W/System.err:     at retrofit2.adapter.rxjava2.CallExecuteObservable.subscribeActual(CallExecuteObservable.java:41)
W/System.err:     at io.reactivex.Observable.subscribe(Observable.java:10179)
W/System.err:     at retrofit2.adapter.rxjava2.BodyObservable.subscribeActual(BodyObservable.java:34)
W/System.err:     at io.reactivex.Observable.subscribe(Observable.java:10179)
W/System.err:     at io.reactivex.internal.operators.observable.ObservableMap.subscribeActual(ObservableMap.java:32)
W/System.err:     at io.reactivex.Observable.subscribe(Observable.java:10179)
W/System.err:     at io.reactivex.internal.operators.observable.ObservableDoOnEach.subscribeActual(ObservableDoOnEach.java:42)
W/System.err:     at io.reactivex.Observable.subscribe(Observable.java:10179)
W/System.err:     at io.reactivex.internal.operators.observable.ObservableDoOnEach.subscribeActual(ObservableDoOnEach.java:42)
W/System.err:     at io.reactivex.Observable.subscribe(Observable.java:10179)
W/System.err:     at io.reactivex.internal.operators.observable.ObservableSubscribeOn$1.run(ObservableSubscribeOn.java:39)
W/System.err:     at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:422)
W/System.err:     at java.util.concurrent.FutureTask.run(FutureTask.java:237)
W/System.err:     at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$201(ScheduledThreadPoolExecutor.java:152)
W/System.err:     at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:265)
W/System.err:     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
W/System.err:     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
W/System.err:     at java.lang.Thread.run(Thread.java:818)
E/AndroidRuntime: FATAL EXCEPTION: main
                  Process: io.app.dev.debug, PID: 17950
                  java.net.SocketTimeoutException: timeout
                      at okio.Okio$4.newTimeoutException(Okio.java:230)
                      at okio.AsyncTimeout.exit(AsyncTimeout.java:275)
                      at okio.AsyncTimeout$2.read(AsyncTimeout.java:243)
                      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.ihsanbal.logging.LoggingInterceptor.intercept(LoggingInterceptor.java:56)
                      at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:92)
                      at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:67)
                      at com.readystatesoftware.chuck.ChuckInterceptor.intercept(ChuckInterceptor.java:172)
                      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:10179)
                      at retrofit2.adapter.rxjava2.BodyObservable.subscribeActual(BodyObservable.java:34)
                      at io.reactivex.Observable.subscribe(Observable.java:10179)
                      at io.reactivex.internal.operators.observable.ObservableMap.subscribeActual(ObservableMap.java:32)
                      at io.reactivex.Observable.subscribe(Observable.java:10179)
                      at io.reactivex.internal.operators.observable.ObservableDoOnEach.subscribeActual(ObservableDoOnEach.java:42)
                      at io.reactivex.Observable.subscribe(Observable.java:10179)
                      at io.reactivex.internal.operators.observable.ObservableDoOnEach.subscribeActual(ObservableDoOnEach.java:42)
                      at io.reactivex.Observable.subscribe(Observable.java:10179)
                      at io.reactivex.internal.operators.observable.ObservableSubscribeOn$1.run(ObservableSubscribeOn.java:39)
                      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)
I/Process: Sending signal. PID: 17950 SIG: 9

【问题讨论】:

贴出调用api获取结果的代码 完成了,但它没有用,因为日志没有显示我的代码中的错误位置,并且在这种情况下 OnError 方法不会触发:( 你能发布完整的代码吗? 什么的完整代码?有一个简单的片段,简单的列表适配器,在我发布时由请求填充,或者瓶颈可能在哪里? sharedSessionDataStorage.cardProvider .getCardProvider() 的完整代码,这可能对理解问题至关重要。 【参考方案1】:

刚刚在 Rx java >2.0 上尝试了这个错误处理程序,看起来这个错误处理程序解决了套接字超时

检查这个答案 [1]:https://***.com/a/54250447/9901309

RX 文档 https://***.com/a/54250447/9901309

【讨论】:

以上是关于SocketTimeoutException 使应用程序崩溃的主要内容,如果未能解决你的问题,请参考以下文章

InfluxDBIOException:java.net.SocketTimeoutException:超时

出现错误:原因:java.net.SocketTimeoutException:接受超时

尝试解决 SSLHandshakeException 时获取 SocketTimeoutException

Jsoup SocketTimeoutException:读取超时

获取 java.net.SocketTimeoutException:android 中的连接超时

SocketTimeoutException Android