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,这样就解决了,但是付出了性能的代价。

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






  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
Android中的网络通信可以通过OkHttpRetrofit来实现。 首先,在你的项目中需要引入OkHttpRetrofit的依赖。你可以在build.gradle文件中添加以下代码来引入依赖: ``` implementation 'com.squareup.okhttp3:okhttp:4.9.0' implementation 'com.squareup.retrofit2:retrofit:2.9.0' ``` 接下来,你需要创建一个OkHttpClient的实例,它将负责处理网络请求。你可以使用OkHttpClient的Builder来配置一些网络请求的参数,比如超时时间、拦截器等。例如: ``` OkHttpClient client = new OkHttpClient.Builder() .connectTimeout(10, TimeUnit.SECONDS) .readTimeout(10, TimeUnit.SECONDS) .build(); ``` 然后,你需要创建一个Retrofit的实例。Retrofit是一个类型安全的HTTP客户端,它可以根据你定义的接口自动生成网络请求的代码。你可以使用Retrofit的Builder来配置一些参数,比如base URL、转换器、适配器等。例如: ``` Retrofit retrofit = new Retrofit.Builder() .baseUrl("https://api.example.com/") .client(client) .addConverterFactory(GsonConverterFactory.create()) .addCallAdapterFactory(RxJava3CallAdapterFactory.create()) .build(); ``` 在这里,我们设置了一个base URL为"https://api.example.com/",使用了GsonConverterFactory来将JSON数据转换为对象,使用了RxJava3CallAdapterFactory来支持RxJava的Observable。 接下来,你需要定义一个接口,用于描述你的网络请求。你可以在接口中定义各种网络请求的方法,并使用注解来指定请求的URL、请求方法、请求参数等。例如: ``` public interface ApiService { @GET("users/{username}") Call<User> getUser(@Path("username") String username); } ``` 然后,你可以使用刚才创建的Retrofit实例来创建一个ApiService的实例,并调用接口中的方法来发起网络请求。例如: ``` ApiService apiService = retrofit.create(ApiService.class); Call<User> call = apiService.getUser("john"); call.enqueue(new Callback<User>() { @Override public void onResponse(Call<User> call, Response<User> response) { if (response.isSuccessful()) { User user = response.body(); // 处理返回的用户数据 } else { // 处理请求失败的情况 } } @Override public void onFailure(Call<User> call, Throwable t) { // 处理请求失败的情况 } }); ``` 以上就是使用OkHttpRetrofit进行网络通信的基本步骤。你可以根据自己的需求来定义更多的网络请求方法,并处理返回的数据或错误。需要注意的是,为了保证网络请求的安全性,你可能还需要在AndroidManifest.xml文件中添加网络权限。例如: ``` <uses-permission android:name="android.permission.INTERNET" /> ``` 希望以上信息对你有所帮助!如果还有其他问题,请随时提问。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值