android – 改造okhttp获取随机eofexception

我正在调用一个调用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) 

我在网上找到了一些关于bug的内容,但我不确定.此外,奇怪的是,有时工作,有时不工作,没有我改变代码中的任何东西.例外情况说明了一个缺少终点线的角色.我必须把它放在URL中吗?这是一些代码.谢谢.

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);

解决方法:

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

希望将来可能对某人有所帮助.

上一篇:android – 如何通过OkHttpClient Interceptor将baseUrl(host)添加到Picasso?


下一篇:java – 为什么okhttp3.Response类是final