【问题标题】:Retrofit okhttp getting random eofexception改造 okhttp 得到随机的 eofexception
【发布时间】:2019-03-05 16:26:52
【问题描述】:

我正在调用一个调用 REST WS 的 Android 应用程序。该服务本身运行良好(使用 Postman 测试),但我偶尔会从 Android 收到此异常:

: java.io.IOException: unexpected end of stream on com.squareup.okhttp.Address@2cd22e9d
                                             at com.squareup.okhttp.internal.http.Http1xStream.readResponse(Http1xStream.java:201)
                                             at com.squareup.okhttp.internal.http.Http1xStream.readResponseHeaders(Http1xStream.java:127)
                                             at com.squareup.okhttp.internal.http.HttpEngine.readNetworkResponse(HttpEngine.java:737)
                                             at com.squareup.okhttp.internal.http.HttpEngine.access$200(HttpEngine.java:87)
                                             at com.squareup.okhttp.internal.http.HttpEngine$NetworkInterceptorChain.proceed(HttpEngine.java:722)
                                             at com.squareup.okhttp.internal.http.HttpEngine.readResponse(HttpEngine.java:576)
                                             at com.squareup.okhttp.Call.getResponse(Call.java:287)
                                             at com.squareup.okhttp.Call$ApplicationInterceptorChain.proceed(Call.java:243)
                                             at com.squareup.okhttp.Call.getResponseWithInterceptorChain(Call.java:205)
                                             at com.squareup.okhttp.Call.execute(Call.java:80)
                                             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$2.obtainResponse(RestAdapter.java:278)
                                             at retrofit.CallbackRunnable.run(CallbackRunnable.java:42)
                                             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:818)
                                          Caused by: java.io.EOFException: \n not found: size=0 content=...
                                             at okio.RealBufferedSource.readUtf8LineStrict(RealBufferedSource.java:201)
                                             at com.squareup.okhttp.internal.http.Http1xStream.readResponse(Http1xStream.java:186)
                                             at com.squareup.okhttp.internal.http.Http1xStream.readResponseHeaders(Http1xStream.java:127) 
                                             at com.squareup.okhttp.internal.http.HttpEngine.readNetworkResponse(HttpEngine.java:737) 
                                             at com.squareup.okhttp.internal.http.HttpEngine.access$200(HttpEngine.java:87) 
                                             at com.squareup.okhttp.internal.http.HttpEngine$NetworkInterceptorChain.proceed(HttpEngine.java:722) 
                                             at com.squareup.okhttp.internal.http.HttpEngine.readResponse(HttpEngine.java:576) 
                                             at com.squareup.okhttp.Call.getResponse(Call.java:287) 
                                             at com.squareup.okhttp.Call$ApplicationInterceptorChain.proceed(Call.java:243) 
                                             at com.squareup.okhttp.Call.getResponseWithInterceptorChain(Call.java:205) 
                                             at com.squareup.okhttp.Call.execute(Call.java:80) 
                                             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$2.obtainResponse(RestAdapter.java:278) 
                                             at retrofit.CallbackRunnable.run(CallbackRunnable.java:42) 
                                             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:818) 

我在网上找到了一些关于错误的信息,但我不确定。此外,奇怪的是有时有效,有时无效,而无需我更改代码中的任何内容。异常说明缺少结束行字符。我必须把它放在网址中吗?这是一些代码。谢谢。

API接口

public interface ApiInterface {

@Headers({"Authorization: Bearer eyJhbGciOiJIUzUxMiJ9.eyJzdWIiOiJkb25hdG8iLCJ1c2VySWQiOiIxIiwicm9sZSI6IlJPTEVfQURNSU4ifQ.yNVtQMfMasfR7nfBgtjtmlXMM8Gpfjvf_FDz1sMiaSJxUvLDyC9sjm-XSEkJdfGy4JasfweRrRUGc3gHpMRA",
            "Accept: application/json",
"Content-Type: application/json;charset=utf-8"})
@POST("/todo/getTodoList")
public void getTodoList(@Body BaseRequest request, Callback<GetTodoListResponse> response);
}

控制器

 RestAdapter restAdapter = new RestAdapter.Builder()
            .setEndpoint("http://www.myUrl.com/app/rest").setLogLevel(RestAdapter.LogLevel.FULL).setConverter(new GsonConverter(builder.create()))
            .setClient(new OkClient(new OkHttpClient()))
            .build();
    ApiInterface methods = restAdapter.create(ApiInterface.class);
    Callback<GetTodoListResponse> callback = new Callback<GetTodoListResponse>() {
        @Override
        public void success(GetTodoListResponse o, Response response) {
            todoList = o.getTodoList();
            Log.d("CardController", "TodoList: " + todoList);
            if (!o.isOutcome()) {
                Log.e("CardController", "Error fetching todoList");
                todoList = new ArrayList<Todo>();
            } else {
                setChanged();
             myMethod();
            }
        }

        @Override
        public void failure(RetrofitError retrofitError) {
            Log.e("Retrofit", "Error fetching todoList: ", retrofitError);
        }
    };
    methods.getTodoList(new AuraBaseRequest(), callback);

【问题讨论】:

  • @Gaurav 感谢您的评论,但正如您所看到的,在我的代码中,我已经在使用该答案中所述的 OkClient,但我还是遇到了问题。我不明白为什么这个错误是不一致的......我在 2 个月后仍在寻找解决方案......
  • @Aurasphere 你能解决这个问题吗?我从 2 天起就被困在这个问题上
  • @BhavikRathod 祝你好运,我在这个问题上困扰了 2 年,但我仍然不知道如何解决它:(
  • @Aurasphere 所以这是唯一的解决方案,如果我收到此问题,我必须重试 API 调用。还有一件事要确认您是否在单个项目中使用了多个不同版本的改造库

标签: android rest retrofit okhttp eofexception


【解决方案1】:

在我的例子中,服务器端是 Spring MVC,我遇到了这个错误。由于某些安全检查(500 返回状态)而发生此错误,但改造中的错误是流意外结束。一旦绕过安全检查,我就能得到响应。

希望它对将来的人有所帮助。

【讨论】:

  • 你是怎么bypass the security check的?
  • @likejudo :我现在不记得了,但我认为它与spring-security.xml有关
猜你喜欢
  • 1970-01-01
  • 2016-01-25
  • 2014-07-12
  • 2015-12-28
  • 2014-03-02
  • 1970-01-01
  • 2018-04-30
  • 1970-01-01
  • 2017-02-03
相关资源
最近更新 更多