改造“java.net.ProtocolException:意外状态行”,有人吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23238397/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 21:27:59  来源:igfitidea点击:

Retrofit "java.net.ProtocolException: Unexpected status line", Anyone?

javaandroidretrofitokhttp

提问by Shawn Thye

My Android app crashed with the following stack trace, but when I try it from a desktop rest client it works well. Does anyone know what the problem might be?

我的 Android 应用程序因以下堆栈跟踪而崩溃,但是当我从桌面休息客户端尝试它时,它运行良好。有谁知道问题可能是什么?

04-23 16:00:50.449  27917-27983/com.iproperty.android.apps.irealtor D/Ireal_IrealtorClient﹕ ---> HTTP GET http://beta2.irealtor.api.iproperty.com.my/Listing?pageSize=10000&orderby=1&order=-1
04-23 16:00:50.449  27917-27983/com.iproperty.android.apps.irealtor D/Ireal_IrealtorClient﹕ If-Modified-Since: 1398235278243
04-23 16:00:50.449  27917-27983/com.iproperty.android.apps.irealtor D/Ireal_IrealtorClient﹕ User-Agent: Android/0.0.20
04-23 16:00:50.449  27917-27983/com.iproperty.android.apps.irealtor D/Ireal_IrealtorClient﹕ Accept: application/json
04-23 16:00:50.449  27917-27983/com.iproperty.android.apps.irealtor D/Ireal_IrealtorClient﹕ Authorization: WFdSeW8vOTJ1Z3BoQlBJMk53VGpaekZRY2pCd1pYSlVXUT090
04-23 16:00:50.449  27917-27983/com.iproperty.android.apps.irealtor D/Ireal_IrealtorClient﹕ ---> END HTTP (0-byte body)
04-23 16:00:50.549  27917-27983/com.iproperty.android.apps.irealtor D/Ireal_IrealtorClient﹕ ---- ERROR http://beta2.irealtor.api.iproperty.com.my/Listing?pageSize=10000&orderby=1&order=-1
04-23 16:00:50.549  27917-27983/com.iproperty.android.apps.irealtor D/Ireal_IrealtorClient﹕ java.net.ProtocolException: Unexpected status line: {"Data":[],"CustomStatusCode":200,"Status":"success"}HTTP/1.1 200 OK
            at com.squareup.okhttp.internal.http.StatusLine.<init>(StatusLine.java:38)
            at com.squareup.okhttp.internal.http.HttpConnection.readResponse(HttpConnection.java:150)
            at com.squareup.okhttp.internal.http.HttpTransport.readResponseHeaders(HttpTransport.java:99)
            at com.squareup.okhttp.internal.http.HttpEngine.readResponse(HttpEngine.java:595)
            at com.squareup.okhttp.internal.http.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:381)
            at com.squareup.okhttp.internal.http.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:328)
            at com.squareup.okhttp.internal.http.HttpURLConnectionImpl.getResponseCode(HttpURLConnectionImpl.java:493)
            at retrofit.client.UrlConnectionClient.readResponse(UrlConnectionClient.java:71)
            at retrofit.client.UrlConnectionClient.execute(UrlConnectionClient.java:38)
            at retrofit.RestAdapter$RestHandler.invokeRequest(RestAdapter.java:358)
            at retrofit.RestAdapter$RestHandler.invoke(RestAdapter.java:284)
            at $Proxy0.syncProperties(Native Method)
            at com.iproperty.android.apps.irealtor.sync.SyncHelper.performSync(SyncHelper.java:210)
            at com.iproperty.android.apps.irealtor.sync.SyncAdapter.onPerformSync(SyncAdapter.java:82)
            at android.content.AbstractThreadedSyncAdapter$SyncThread.run(AbstractThreadedSyncAdapter.java:259)
04-23 16:00:50.549  27917-27983/com.iproperty.android.apps.irealtor D/Ireal_IrealtorClient﹕ ---- END ERROR


/**Sample code by using BasicHttpClient*/
BasicHttpClient httpClientTest = new BasicHttpClient();
httpClientTest.addHeader("If-Modified-Since", "1398312582192");
httpClientTest.addHeader("Authorization", "WFdSeW8vOTJ1Z3BoQlBJMk53VGpaekZRY2pCd1pYSlVXUT090");
httpClientTest.addHeader("User-Agent", "Android/0.0.20");
httpClientTest.addHeader("Accept", "application/json");

HttpResponse responseTest = httpClientTest.get("http://beta2.irealtor.api.iproperty.com.my/Listing?pageSize=100&orderby=1&order=-1",null);
final int statusTest = responseTest.getStatus(); // here i get 204, because i make server return 204 then there is nothing.
String body responseTest.getAsString() // here i get empty which is correct.

采纳答案by Jesse Wilson

Something is causing problems with the response body length of the preceding HTTP request, and that's causing the pooled connection to become corrupted.

某些东西会导致前面 HTTP 请求的响应正文长度出现问题,从而导致池连接损坏。

The preceding request was interpreted by OkHttp to have no body, but it has this body:

前面的请求被 OkHttp 解释为没有正文,但它有这个正文:

{"Data":[],"CustomStatusCode":200,"Status":"success"}

It's probably either a bug in the server, or a bug in OkHttp, depending on which headers and code was sent by the preceding URL. If you can paste the request that precedes this failure, we can figure out whose fault it is!

这可能是服务器中的错误,或者 OkHttp 中的错误,具体取决于前面的 URL 发送了哪些标头和代码。如果您可以粘贴此失败之前的请求,我们就可以找出是谁的错!

回答by freebiefalk

Also getting this response error when sending two POST requests in quick succession. The solution is to add header "Connection: close" for both of these requests.

快速连续发送两个 POST 请求时也会收到此响应错误。解决方案是为这两个请求添加标头“Connection: close”。

回答by Pablo Johnson

i add a network interceptor and add the close connection header to it for the okhttp3 client.

我添加了一个网络拦截器,并为 okhttp3 客户端添加了关闭连接头。

okHttpClient = new OkHttpClient.Builder()
                .addNetworkInterceptor(new Interceptor() {
                    @Override
                    public Response intercept(Chain chain) throws IOException {
                        Request request = chain.request().newBuilder().addHeader("Connection", "close").build();
                        return chain.proceed(request);
                    }
                })
                .build();

And that solve the problem. Hope it helps!

这解决了问题。希望能帮助到你!

回答by vinay

You can fix this by setting the header value @Headers("Connection:close")on your retrofit method.
This worked for me.

您可以通过@Headers("Connection:close")在改造方法上设置标题值来解决此问题。
这对我有用。