Okhttp not selecting http2 on Android 7.0

混江龙づ霸主 提交于 2019-12-11 04:39:53

问题


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

The code i use is as follows:

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

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

As I notice in the response object, the protocol used by okHttp is 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

I am using okhttp v3.5

What am i possibly missing out here ?


回答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);

https://android.googlesource.com/platform/external/okhttp/+/master/android/main/java/com/squareup/okhttp/HttpsHandler.java#75



来源:https://stackoverflow.com/questions/44122781/okhttp-not-selecting-http2-on-android-7-0

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