Retrofit java.net.ProtocolException: Expected ':status' header not present

故事扮演 提交于 2019-12-11 02:44:55

问题


Retrofit network calls fails with a Protocol Exception suddenly in a working app. The app was working till yesterday and today all the network calls fails. The calls works fine with HTTP and with some HTTPS except my production endpoint.

It seems that app is not working only on my used https endpoint but working with other https endpoint, I tried.

I fixed the problem this way -

OkHttpClient client = new OkHttpClient();
client.setProtocols(Arrays.asList(Protocol.HTTP_1_1));

And

And my problem solved and except this after upgrading my used library okhttp2 to okhttp3 also working fine with same https endpoint.

  1. I don't understand what can be the real cause?
  2. And if it's problem with okhttp2 then why it was working recently?
  3. And why it's problem on a specific endpoint and working fine for some endponits?

I followed this and this link but still I don't understand whats the real problem.

Note 1: - This is working fine in Samsung S4 (Android verison 4.4.2) device. and in logs showing "OkHttp-Selected-Protocol: http/1.1" but it's not working in Android 5.1.1, 6.0.1, 7.1 & Android 8.1.0

Note 2: - It was working perfectly without setting any explicitly HTTP1.1 protocol but it suddenly stopped working (in device 5.1.1, 6.0.1, 7.1 & Android 8.1.0).

Here is the logs,

java.net.ProtocolException: Expected ':status' header not present at
com.squareup.okhttp.internal.http.SpdyTransport.readNameValueBlock(SpdyTransport.java:197)at com.squareup.okhttp.internal.http.SpdyTransport.readResponseHeaders(SpdyTransport.java:104)
at com.squareup.okhttp.internal.http.HttpEngine.readNetworkResponse(HttpEngine.java:917)
at com.squareup.okhttp.internal.http.HttpEngine.access$300(HttpEngine.java:95)
at com.squareup.okhttp.internal.http.HttpEngine$NetworkInterceptorChain.proceed(HttpEngine.java:902)
at com.squareup.okhttp.internal.http.HttpEngine.readResponse(HttpEngine.java:760)
at com.squareup.okhttp.Call.getResponse(Call.java:274)
at com.squareup.okhttp.Call$ApplicationInterceptorChain.proceed(Call.java:230)
at com.squareup.okhttp.Call.getResponseWithInterceptorChain(Call.java:201)                                                                                         
at com.squareup.okhttp.Call.execute(Call.java:81)
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:1113)                                                                                     
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)                                                                                      
at retrofit.Platform$Android$2$1.run(Platform.java:142)
at java.lang.Thread.run(Thread.java:818)

回答1:


Your crash is coming from SpdyTransport.java which is the clue to what is happening. SPDY has been deprecated and is being phased out. A change in server configuration is the most like explanation for what you are seeing. Probably an update that drops or modifies support for SPDY. Upgrading to okhttp3 is the correct fix for your app. Okhttp3 does not support SPDY, so will not try to make a SPDY connection. The reason the old works with some endpoints and not others is explained by the servers running different software and having different configurations.

The reason it worked and continued to work on your 4.4.2 device is that android did not support SPDY until 5.0, so that device never attempted to use a SPDY connection.




回答2:


Note : If you are using both apache httpmime and retrofit simultaneously Use apache httpmime older version.

(eg : in my case i used this to upload a image on server and also to get the content lenghth/image size)

This his an older version (not so old) or prev version, You should implement same old version for both.

implementation 'com.squareup.retrofit2:retrofit:2.0.2' implementation 'org.apache.httpcomponents:httpmime:4.3.6'

This his an new version of retrofit, You should use old version apache httpmime.

implementation 'com.squareup.retrofit2:retrofit:2.3.0' implementation 'org.apache.httpcomponents:httpmime:4.3.6'

Thank You so much, If u loved my solution plz voteup for me.



来源:https://stackoverflow.com/questions/49531342/retrofit-java-net-protocolexception-expected-status-header-not-present

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!