使用Okhttp访问网络可能出现的bug——EOFException异常

题引:同事跟我报告了一个bug,说是我的代码可能有问题,因为某个页面偶尔会请求不到数据。刚好他的手机遇到了这个bug,果断把他手机拿过来debug。
  • bug
  • 解决方案
  • Refer

一、bug

java.io.IOException: unexpected end of stream on Connection{10.144.59.130:8291, proxy=DIRECT@ hostAddress=10.144.59.130 cipherSuite=none protocol=http/1.1} (recycle count=9)

at com.squareup.okhttp.internal.http.HttpConnection.readResponse(HttpConnection.java:211)

at com.squareup.okhttp.internal.http.HttpTransport.readResponseHeaders(HttpTransport.java:80)

at com.squareup.okhttp.internal.http.HttpEngine.readNetworkResponse(HttpEngine.java:906)

at com.squareup.okhttp.internal.http.HttpEngine.access$300(HttpEngine.java:92)

at com.squareup.okhttp.internal.http.HttpEngine$NetworkInterceptorChain.proceed(HttpEngine.java:891)

at com.squareup.okhttp.internal.http.HttpEngine.readResponse(HttpEngine.java:749)

at com.squareup.okhttp.Call.getResponse(Call.java:268)

at com.squareup.okhttp.Call$ApplicationInterceptorChain.proceed(Call.java:224)

at com.sogou.bizdev.mobileyunguan.api.ReceivedCookiesInterceptor.intercept(ReceivedCookiesInterceptor.java:19)

at com.squareup.okhttp.Call$ApplicationInterceptorChain.proceed(Call.java:221)

at com.squareup.okhttp.Call.getResponseWithInterceptorChain(Call.java:195)

at com.squareup.okhttp.Call.execute(Call.java:79)

at retrofit.client.OkClient.execute(OkClient.java:53)

at retrofit.RestAdapter$RestHandler.invokeRequest(RestAdapter.java:326)

at retrofit.RestAdapter$RestHandler.access$100(RestAdapter.java:220)

at retrofit.RestAdapter$RestHandler$1.invoke(RestAdapter.java:265)

at retrofit.RxSupport$2.run(RxSupport.java:55)

at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:422)

at java.util.concurrent.FutureTask.run(FutureTask.java:237)

at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)

at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)

at retrofit.Platform$Android$2$1.run(Platform.java:142)

at java.lang.Thread.run(Thread.java:841)

Caused by: java.io.EOFException: \n not found: size=0 content=...

at okio.RealBufferedSource.readUtf8LineStrict(RealBufferedSource.java:201)

at com.squareup.okhttp.internal.http.HttpConnection.readResponse(HttpConnection.java:191)

at com.squareup.okhttp.internal.http.HttpTransport.readResponseHeaders(HttpTransport.java:80)?

at com.squareup.okhttp.internal.http.HttpEngine.readNetworkResponse(HttpEngine.java:906)?

at com.squareup.okhttp.internal.http.HttpEngine.access$300(HttpEngine.java:92)?

at com.squareup.okhttp.internal.http.HttpEngine$NetworkInterceptorChain.proceed(HttpEngine.java:891)?

at com.squareup.okhttp.internal.http.HttpEngine.readResponse(HttpEngine.java:749)?

at com.squareup.okhttp.Call.getResponse(Call.java:268)?

at com.squareup.okhttp.Call$ApplicationInterceptorChain.proceed(Call.java:224)?

at com.sogou.bizdev.mobileyunguan.api.ReceivedCookiesInterceptor.intercept(ReceivedCookiesInterceptor.java:19)?

at com.squareup.okhttp.Call$ApplicationInterceptorChain.proceed(Call.java:221)?

at com.squareup.okhttp.Call.getResponseWithInterceptorChain(Call.java:195)?

at com.squareup.okhttp.Call.execute(Call.java:79)?

at retrofit.client.OkClient.execute(OkClient.java:53)?

at retrofit.RestAdapter$RestHandler.invokeRequest(RestAdapter.java:326)?

at retrofit.RestAdapter$RestHandler.access$100(RestAdapter.java:220)?

at retrofit.RestAdapter$RestHandler$1.invoke(RestAdapter.java:265)?

at retrofit.RxSupport$2.run(RxSupport.java:55)?

at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:422)?

at java.util.concurrent.FutureTask.run(FutureTask.java:237)?

at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)?

at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)?

at retrofit.Platform$Android$2$1.run(Platform.java:142)?

at java.lang.Thread.run(Thread.java:841)?

---- END ERROR

unexpected end of stream on Connection{121.42.22.106:8381, proxy=DIRECT@ hostAddress=121.42.22.106 cipherSuite=none protocol=http/1.1} (recycle count=9)

retrofit.RetrofitError: unexpected end of stream on Connection{121.42.22.106:8381, proxy=DIRECT@ hostAddress=121.42.22.106 cipherSuite=none protocol=http/1.1} (recycle count=9)

at retrofit.RestAdapter$RestHandler.invokeRequest(RestAdapter.java:395)

at retrofit.RestAdapter$RestHandler.access$100(RestAdapter.java:220)

at retrofit.RestAdapter$RestHandler$1.invoke(RestAdapter.java:265)

at retrofit.RxSupport$2.run(RxSupport.java:55)

at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:422)

at java.util.concurrent.FutureTask.run(FutureTask.java:237)

at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)

at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)

at retrofit.Platform$Android$2$1.run(Platform.java:142)

at java.lang.Thread.run(Thread.java:841)

Caused by: java.io.IOException: unexpected end of stream on Connection{121.42.22.106:8381, proxy=DIRECT@ hostAddress=121.42.22.106 cipherSuite=none protocol=http/1.1} (recycle count=9)

at com.squareup.okhttp.internal.http.HttpConnection.readResponse(HttpConnection.java:211)

at com.squareup.okhttp.internal.http.HttpTransport.readResponseHeaders(HttpTransport.java:80)

at com.squareup.okhttp.internal.http.HttpEngine.readNetworkResponse(HttpEngine.java:906)

at com.squareup.okhttp.internal.http.HttpEngine.access$300(HttpEngine.java:92)

at com.squareup.okhttp.internal.http.HttpEngine$NetworkInterceptorChain.proceed(HttpEngine.java:891)

at com.squareup.okhttp.internal.http.HttpEngine.readResponse(HttpEngine.java:749)

at com.squareup.okhttp.Call.getResponse(Call.java:268)

at com.squareup.okhttp.Call$ApplicationInterceptorChain.proceed(Call.java:224)

at com.sogou.bizdev.mobileyunguan.api.ReceivedCookiesInterceptor.intercept(ReceivedCookiesInterceptor.java:19)

at com.squareup.okhttp.Call$ApplicationInterceptorChain.proceed(Call.java:221)

at com.squareup.okhttp.Call.getResponseWithInterceptorChain(Call.java:195)

at com.squareup.okhttp.Call.execute(Call.java:79)

at retrofit.client.OkClient.execute(OkClient.java:53)

at retrofit.RestAdapter$RestHandler.invokeRequest(RestAdapter.java:326)

at retrofit.RestAdapter$RestHandler.access$100(RestAdapter.java:220)?

at retrofit.RestAdapter$RestHandler$1.invoke(RestAdapter.java:265)?

at retrofit.RxSupport$2.run(RxSupport.java:55)?

at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:422)?

at java.util.concurrent.FutureTask.run(FutureTask.java:237)?

at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)?

at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)?

at retrofit.Platform$Android$2$1.run(Platform.java:142)?

at java.lang.Thread.run(Thread.java:841)?

Caused by: java.io.EOFException: \n not found: size=0 content=...

at okio.RealBufferedSource.readUtf8LineStrict(RealBufferedSource.java:201)

at com.squareup.okhttp.internal.http.HttpConnection.readResponse(HttpConnection.java:191)

at com.squareup.okhttp.internal.http.HttpTransport.readResponseHeaders(HttpTransport.java:80)?

at com.squareup.okhttp.internal.http.HttpEngine.readNetworkResponse(HttpEngine.java:906)?

at com.squareup.okhttp.internal.http.HttpEngine.access$300(HttpEngine.java:92)?

at com.squareup.okhttp.internal.http.HttpEngine$NetworkInterceptorChain.proceed(HttpEngine.java:891)?

at com.squareup.okhttp.internal.http.HttpEngine.readResponse(HttpEngine.java:749)?

at com.squareup.okhttp.Call.getResponse(Call.java:268)?

at com.squareup.okhttp.Call$ApplicationInterceptorChain.proceed(Call.java:224)?

at com.sogou.bizdev.mobileyunguan.api.ReceivedCookiesInterceptor.intercept(ReceivedCookiesInterceptor.java:19)?

at com.squareup.okhttp.Call$ApplicationInterceptorChain.proceed(Call.java:221)?

at com.squareup.okhttp.Call.getResponseWithInterceptorChain(Call.java:195)?

at com.squareup.okhttp.Call.execute(Call.java:79)?

at retrofit.client.OkClient.execute(OkClient.java:53)?

at retrofit.RestAdapter$RestHandler.invokeRequest(RestAdapter.java:326)?

at retrofit.RestAdapter$RestHandler.access$100(RestAdapter.java:220)?

at retrofit.RestAdapter$RestHandler$1.invoke(RestAdapter.java:265)?

at retrofit.RxSupport$2.run(RxSupport.java:55)?

at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:422)?

at java.util.concurrent.FutureTask.run(FutureTask.java:237)?

at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)?

at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)?

at retrofit.Platform$Android$2$1.run(Platform.java:142)?

at java.lang.Thread.run(Thread.java:841)?

二、解决方案

在请求头中添加做如下处理:关闭连接,不让它保持连接。

if (Build.VERSION.SDK != null && Build.VERSION.SDK_INT > 13) {
request.addHeader("Connection", "close");
}

三、Refer

1、记一次使用Okhttp遇到的坑

2、android用okhttp和retrofit访问网络的时候有时候出现EOFException异常

你可能感兴趣的:(Android-APP)