java okhttp3异常_okhttp3 出现 .IOException: unexpec

在调用 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。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值