Okhttp在Android 7.0上未选择http2 [英] Okhttp not selecting http2 on Android 7.0

查看:255
本文介绍了Okhttp在Android 7.0上未选择http2的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在Android 7.0上运行的设备上向以下域(已启用http2)发出HTTP请求.

I am trying to make an HTTP request to the following domain (http2 enabled) on a device running on Android 7.0.

我使用的代码如下:

Request request = new Request.Builder()
                .url("https://http2.akamai.com/")
                .build();

response = okHttpClient.newCall(request).execute();
statusCode = response.code();

正如我在响应对象中注意到的那样,okHttp使用的协议是HTTP 1.1

As I notice in the response object, the protocol used by okHttp is HTTP 1.1

okhttp客户端的行为是随机的,一次我能够看到协议为h2,但是随后重复请求,它一直选择HTTP 1.1

The behavior of the okhttp client is random, at one time I was able to see the protocol as h2 but then repeating the request, it kept selecting HTTP 1.1

我正在使用okhttp v3.5

I am using okhttp v3.5

我在这里可能会错过什么?

What am i possibly missing out here ?

推荐答案

Android在其OkHttp中仅启用了HTTP/1.1.

Android has enabled only HTTP/1.1 in its OkHttp.

/**
 * Creates an OkHttpClient suitable for creating HttpsURLConnection instances on
 * Android.
 */
public static OkUrlFactory createHttpsOkUrlFactory(Proxy proxy) {
  ...
  // Only enable HTTP/1.1 (implies HTTP/1.0). Disable SPDY / HTTP/2.0.
  okHttpClient.setProtocols(HTTP_1_1_ONLY);

查看全文

登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆