问题
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