如何在Android设备上使用http / 2和Okhttp? [英] How to use http/2 with Okhttp on Android devices?
本文介绍了如何在Android设备上使用http / 2和Okhttp?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在测试支持HTTP / 2的网站,这样的,
和我尝试使用okhttp发送请求:
I'M testing a site that supports HTTP/2,like this, and I try to use okhttp to send the request:
OkHttpClient okHttpClient = new OkHttpClient();
Request request = new Request.Builder()
.url("https://www.google.it")
.build();
okHttpClient.newCall(request).enqueue(new Callback() {
@Override
public void onFailure(Request request, IOException e) {
e.printStackTrace();
}
@Override
public void onResponse(Response response) throws IOException {
Log.d("TestHttp", "OkHttp-Selected-Protocol: " + response.header("OkHttp-Selected-Protocol"));
Log.d("TestHttp", "Response code is " + response.code());
}
});
在日志中我得到这样的结果:
In the log I got something like this:
OkHttp-Selected-Protocol: http/1.1
okhttpClient选择使用http / 1.1,如何强制它使用HTTP / 2?
The okhttpClient chose to use http/1.1, how can I force it to use HTTP/2?
推荐答案
Okhttp 2.5+仅支持http / 2通过ALPN高于5.0+。
Okhttp 2.5+ only support http/2 above 5.0+ via ALPN.
但您可以修改源代码以通过NPN支持高于4.0+的http / 2.
but you can modify the source code to support http/2 above 4.0+ via NPN.
这篇关于如何在Android设备上使用http / 2和Okhttp?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文