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

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


 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{10.144.59.130:8291, proxy=DIRECT@ hostAddress=10.144.59.130 cipherSuite=none protocol=http/1.1} (recycle count=9)
 retrofit.RetrofitError: 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 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{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)?



找了半天在StackOverFlow上找到了答案,虽然答案不是太理想,但是确实解决了这个问题,在此记录一下


答案如下:

This was not a well documented answer. It appears in some of the newer versions of android, there is a bug with recycled url connections. To fix this (although there may be some performance issues), I needed to add:

if (Build.VERSION.SDK != null && Build.VERSION.SDK_INT > 13) {
    urlConnect.setRequestProperty("Connection", "close");
}
或:
if (Build.VERSION.SDK != null && Build.VERSION.SDK_INT > 13) {
    request.addHeader("Connection", "close");
}
主要就是在http header里面增加关闭连接,不让它保持连接。
主要是在回收url connection有可能有问题,后来我也增加了连接关闭,不保持url connection,这样就解决了,但是付出了性能的代价。

继续查找看有没有更好的办法。






你可能感兴趣的:(移动开发)