相关疑难解决方法(0)

HTTP/2协议不能与okhttp一起使用

我正在使用带有okhttp 2.4.0的Retrofit 1.9.到目前为止,我们在服务器端禁用了SPDY协议(我通过检查).服务器端的启用协议是HTTP/2(我通过检查).

所以我认为okhttp将尝试使用HTTP/2(最新的一个协议)进行api调用,但它在Android设备4.2.2 samsung S4上使用HTTP/1.1 -

D/Retrofit : OkHttp-Selected-Protocol: http/1.1
Run Code Online (Sandbox Code Playgroud)

有人告诉我Android设备直到5.0 才支持SPDY(我没有任何证据),因此该设备从未尝试过使用SPDY连接.但我确信我们的服务器根本没有使用SPDY我自己使用上面的链接检查了它.

并且okhttp开始支持HTTP/2,因为它是2.3.0版本,请参见此处.

如果我在上面的Android 5.0中使用相同的东西,我在生产应用程序上获得了相同配置(okhttp 2.4.0和Retrofit 1.9)的异常 -

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)
Run Code Online (Sandbox Code Playgroud)

有两个修复 -

  1. 强制okhttp只使用http/1.1 -

    OkHttpClient客户端=新的OkHttpClient(); client.setProtocols(Arrays.asList(Protocol.HTTP_1_1));

注意 - 我们不能使用Protocol.HTTP_2或Protocol.SPDY_3,因为如果 …

android retrofit okhttp retrofit2 okhttp3

14
推荐指数
0
解决办法
1473
查看次数

标签 统计

android ×1

okhttp ×1

okhttp3 ×1

retrofit ×1

retrofit2 ×1