okhttp3 出现 .IOException: unexpected end of stream on Connection 问题的解决方法

在调用 okhttp3 时抛出如下异常:

D/NetworkSecurityConfig: No Network Security Config specified, using platform default
W/System.err: java.io.IOException: unexpected end of stream on Connection{mirrors.shu.edu.cn:80, proxy=DIRECT hostAddress=mirrors.shu.edu.cn/202.121.199.235:80 cipherSuite=none protocol=http/1.1}
W/System.err:     at okhttp3.internal.http1.Http1Codec.readResponseHeaders(Http1Codec.java:208)
W/System.err:     at okhttp3.internal.http.CallServerInterceptor.intercept(CallServerInterceptor.java:88)
W/System.err:     at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147)
W/System.err:     at okhttp3.internal.connection.ConnectInterceptor.intercept(ConnectInterceptor.java:45)
W/System.err:     at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147)
W/System.err:     at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:121)
W/System.err:     at okhttp3.internal.cache.CacheInterceptor.intercept(CacheInterceptor.java:93)
W/System.err:     at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147)
W/System.err:     at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:121)
W/System.err:     at okhttp3.internal.http.BridgeInterceptor.intercept(BridgeInterceptor.java:93)
W/System.err:     at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147)
W/System.err:     at okhttp3.internal.http.RetryAndFollowUpInterceptor.intercept(RetryAndFollowUpInterceptor.java:126)
W/System.err:     at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147)
W/System.err:     at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:121)
W/System.err:     at okhttp3.RealCall.getResponseWithInterceptorChain(RealCall.java:200)
W/System.err:     at okhttp3.RealCall.execute(RealCall.java:77)
W/System.err:     at net.deniro.android.servicebestpractice.DownloadTask.getContentLength(DownloadTask.java:202)
W/System.err:     at net.deniro.android.servicebestpractice.DownloadTask.doInBackground(DownloadTask.java:80)
W/System.err:     at net.deniro.android.servicebestpractice.DownloadTask.doInBackground(DownloadTask.java:22)
W/System.err:     at android.os.AsyncTask$2.call(AsyncTask.java:304)
W/System.err:     at java.util.concurrent.FutureTask.run(FutureTask.java:237)
W/System.err:     at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:243)
W/System.err:     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1133)
W/System.err:     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:607)
W/System.err:     at java.lang.Thread.run(Thread.java:761)
W/System.err: Caused by: java.io.EOFException: \n not found: limit=1 content=0d…
W/System.err:     at okio.RealBufferedSource.readUtf8LineStrict(RealBufferedSource.java:237)
W/System.err:     at okhttp3.internal.http1.Http1Codec.readHeaderLine(Http1Codec.java:215)
W/System.err:     at okhttp3.internal.http1.Http1Codec.readHeaders(Http1Codec.java:224)
W/System.err:     at okhttp3.internal.http1.Http1Codec.readResponseHeaders(Http1Codec.java:195)
W/System.err:   ... 24 more

通过以下方法来构建出 OkHttpClient,一般就不会出现上面的问题啦:

 private OkHttpClient buildHttpClient() {
        return new OkHttpClient.Builder().retryOnConnectionFailure(true).connectTimeout(30, TimeUnit.SECONDS).build();
    }

这里通过 Builder 对 OkHttpClient 进行以下设置:

  • retryOnConnectionFailure - 当连接失败,尝试重连。
  • connectTimeout - 设置连接超时时间,这里设置为 30 s。

你可能感兴趣的:(okhttp3 出现 .IOException: unexpected end of stream on Connection 问题的解决方法)