【问题标题】:ProtocolException: Expected ':status' header not presentProtocolException:预期的“:状态”标头不存在
【发布时间】:2018-03-30 03:41:57
【问题描述】:

改造网络调用在工作应用中突然出现协议异常而失败。 该应用程序一直工作到昨天,今天所有的网络调用都失败了。调用在 HTTP 上运行良好,但在 HTTPS 下失败。

这是日志,

java.net.ProtocolException: Expected ':status' header not present
10-18 14:59:01.103 30746-30746/? W/System.err:     at okhttp3.internal.http.Http2xStream.readHttp2HeadersList(Http2xStream.java:262)
10-18 14:59:01.103 30746-30746/? W/System.err:     at okhttp3.internal.http.Http2xStream.readResponseHeaders(Http2xStream.java:145)
10-18 14:59:01.103 30746-30746/? W/System.err:     at okhttp3.internal.http.CallServerInterceptor.intercept(CallServerInterceptor.java:53)
10-18 14:59:01.103 30746-30746/? W/System.err:     at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:92)
10-18 14:59:01.103 30746-30746/? W/System.err:     at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:67)
10-18 14:59:01.103 30746-30746/? W/System.err:     at codmob.com.campuswallet.app.ApiClient$1.intercept(ApiClient.java:66)
10-18 14:59:01.103 30746-30746/? W/System.err:     at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:92)
10-18 14:59:01.103 30746-30746/? W/System.err:     at okhttp3.internal.connection.ConnectInterceptor.intercept(ConnectInterceptor.java:45)
10-18 14:59:01.103 30746-30746/? W/System.err:     at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:92)
10-18 14:59:01.103 30746-30746/? W/System.err:     at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:67)
10-18 14:59:01.103 30746-30746/? W/System.err:     at okhttp3.internal.cache.CacheInterceptor.intercept(CacheInterceptor.java:109)
10-18 14:59:01.103 30746-30746/? W/System.err:     at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:92)
10-18 14:59:01.103 30746-30746/? W/System.err:     at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:67)
10-18 14:59:01.103 30746-30746/? W/System.err:     at okhttp3.internal.http.BridgeInterceptor.intercept(BridgeInterceptor.java:93)
10-18 14:59:01.103 30746-30746/? W/System.err:     at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:92)
10-18 14:59:01.103 30746-30746/? W/System.err:     at okhttp3.internal.http.RetryAndFollowUpInterceptor.intercept(RetryAndFollowUpInterceptor.java:124)
10-18 14:59:01.103 30746-30746/? W/System.err:     at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:92)
10-18 14:59:01.104 30746-30746/? W/System.err:     at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:67)
10-18 14:59:01.104 30746-30746/? W/System.err:     at okhttp3.RealCall.getResponseWithInterceptorChain(RealCall.java:170)
10-18 14:59:01.104 30746-30746/? W/System.err:     at okhttp3.RealCall.access$100(RealCall.java:33)
10-18 14:59:01.104 30746-30746/? W/System.err:     at okhttp3.RealCall$AsyncCall.execute(RealCall.java:120)
10-18 14:59:01.104 30746-30746/? W/System.err:     at okhttp3.internal.NamedRunnable.run(NamedRunnable.java:32)
10-18 14:59:01.104 30746-30746/? W/System.err:     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1133)
10-18 14:59:01.104 30746-30746/? W/System.err:     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:607)
10-18 14:59:01.104 30746-30746/? W/System.err:     at java.lang.Thread.run(Thread.java:761)

【问题讨论】:

标签: android retrofit retrofit2 okhttp3 protocolexception


【解决方案1】:

经过几个小时的混乱,终于找到了解决方案。将 Retrofit 和 Okhttp3 库更新到最新版本对我有用。

compile 'com.squareup.okhttp3:okhttp:3.9.0'

compile 'com.squareup.retrofit2:retrofit:2.3.0'

【讨论】:

  • 通过使用这个修复我的问题仍然存在。
  • 如果您的应用处于生产状态,则无法解决此问题 - 用户在更新之前会继续遇到问题。 @aradon 回答解释了原因,更合适的是降级 nginx 和更新 oktthp。
【解决方案2】:

我正在使用OkHttp2 (2.7.5),我通过强制客户端使用HTTP 1.1 协议解决了这个问题

OkHttpClient client = new OkHttpClient();
client.setProtocols(Arrays.asList(Protocol.HTTP_1_1)); // <- add this line

【讨论】:

  • 什么意思?
  • @BajrangHudda 这行意味着您的OkHttp 客户端在可用时不会尝试使用http2 协议。您只需将此行放入您的客户端应用程序源代码中,然后将新版本发布到生产环境中。就是这样。
  • 好的,我明白了。但是为什么应用程序突然停止工作?昨天它在同一台设备上工作,但今天它只能通过设置 Protocol.Http.1.1 工作。为什么会这样?
  • @BajrangHudda 也许您或其他人将服务器引擎更新到支持http2 协议的版本。这就是我的原因。
  • @BajrangHudda 这是我的想法:http2 是新协议,似乎较旧的 Android 不支持它 - 所以 OkHttp2 仅使用 http1.1 运行良好。较新的 Android 版本支持 http2OkHttp2(它不能很好地处理 http2)做出决定“让我们使用这个新的 http2 协议,因为它在服务器上可用” - 这就是它不起作用的原因。
【解决方案3】:

今天也遇到了同样的问题。原因是将服务器上的 nginx 更新到最新版本(1.13.6)。如果他们没有更新服务器上的 nginx,请询问您的后端团队。

nginx 更新日志 - http://nginx.org/en/CHANGES

【讨论】:

  • 将链接页面的相关部分发布在答案中,这样即使页面发生变化,答案仍然有用。 stackoverflow.com/help/how-to-answer
  • 更新到最新版本的 okhttp3 (3.9.0) 和 retrofit2 (2.3,0) 对我有用。
  • 但是我用的是sucuri,可能是什么原因?
【解决方案4】:

我用的是okhttp3(okhttp-3.4.1),okhttp3对HTTP_1.1协议不是很兼容,需要手动添加。你可以看到Official link

OkHttpClient.Builder builder = new OkHttpClient.Builder();
//protocols
List<Protocol> protocols = new ArrayList<Protocol>();
protocols.add(Protocol.HTTP_1_1);
protocols.add(Protocol.HTTP_2);
builder.protocols(protocols);

【讨论】:

    猜你喜欢
    • 2018-07-24
    • 2018-09-07
    • 2018-09-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-22
    相关资源
    最近更新 更多