android用okhttp和retrofit访问网络的时候有时候出现EOFException异常
2016-03-30 20:15
204 查看
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) {
request.addHeader("Connection", "close");
}[/code]
继续查找看有没有更好的办法。
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");
}[/code]
主要就是在http header里面增加关闭连接,不让它保持连接。
主要是在回收url connection有可能有问题,后来我也增加了连接关闭,不保持url connection,这样就解决了,但是付出了性能的代价。
继续查找看有没有更好的办法。
相关文章推荐
- 实例说明TCP和UDP有什么区别
- 【Android】Android客户端使用okhttp上传文件php服务器
- java-入门:模拟网络购物支付系统
- ASP.NET网络编程学习第一步:清晰概念
- 在android中使用HttpURLConnection进行文件上传
- 《神经网络与深度学习》的教学方法
- 常用的http请求header
- http协议通信过程和请求格式
- 网络40ms延迟问题
- springMVC:org.springframework.web.servlet.PageNotFound.handleHttpRequestMethodNotSupported Request method 'POST' not supported
- Swift 2.0 异步网络请求从网页获取json,并转化为NSArray,遍历得到其中数据.
- Ruby程序中发送基于HTTP协议的请求的简单示例
- 使用 libevent 和 libev 提高网络应用性能——I/O模型演进变
- 卷积神经网络和CIFAR-10:Yann LeCun专访 Convolutional Nets and CIFAR-10: An Interview with Yann LeCun
- Python内置的HTTP协议服务器SimpleHTTPServer使用指南
- zabbix3.0_网络发现问题
- SDN定义网络
- iOS 关闭强制使用https
- 一、cocos2d-x 3.0 final使用httpclient编译到android,须要用到的android.mk
- 网络编程基础学习文章整合