使用OkHttp3和Retrofit2实现HTTP/2

5

我花费了很多时间让OkHttp3支持HTTP/2,但是我已经没有更多的想法了。

如何检查是否可用:

在access.log中检查连接到服务器的客户端使用哪种协议: 浏览器: "GET /favicon.ico HTTP/2.0" 200 382 https://server.com "" Mozilla / 5.0 (X11; Linux x86_64)" Android客户端: 'GET /api/v2/ ... HTTP/1.1 "200 3717" - "" Android ..."

我使用的工具:

服务器版本为nginx/1.10.2https JAVA_VERSION="1.8.0_76" Android版本="6.0.1"

build.gradle

compile 'com.facebook.stetho:stetho-okhttp3:1.4.2'
compile 'com.squareup.okhttp3:okhttp:3.5.0'
compile 'com.squareup.retrofit2:retrofit:2.1.0'
compile 'com.squareup.retrofit2:adapter-rxjava:2.1.0'
...

如何使用:

 client = configureClient(new OkHttpClient().newBuilder())
          .connectTimeout(CONNECT_TIMEOUT, TimeUnit.SECONDS)
          .writeTimeout(WRITE_TIMEOUT, TimeUnit.SECONDS)
          .readTimeout(READ_TIMEOUT, TimeUnit.SECONDS)
          .protocols(Arrays.asList(Protocol.HTTP_2, Protocol.SPDY_3, Protocol.HTTP_1_1))
          .addInterceptor(new Interceptor() {
              @Override
              public Response intercept(Chain chain) throws IOException {
                ...
              }
          })
          .addNetworkInterceptor(new StethoInterceptor())
          .build();

我的尝试

我发现需要将Jetty ALPN jar添加到我的引导类路径中。 因此,我在我的build.gradle(Project)文件中添加了以下代码行:

allprojects {
  ...
    gradle.projectsEvaluated {
        tasks.withType(JavaCompile) {
          options.compilerArgs.add('-Xbootclasspath/p:/home/USER_NAME/Android/alpn-boot/alpn-boot-8.1.2.v20141202.jar')
        }
}

我尝试了所有来自以下链接的alpn-boot版本: https://mvnrepository.com/artifact/org.mortbay.jetty.alpn/alpn-boot 但是没有成功,甚至出现错误信息。
我还尝试在build.gradle中添加 compile("org.mortbay.jetty.alpn:alpn-boot:VERSION_NUMBER") ,但是在版本7*中仍然没有错误并且不起作用。在版本8*中构建应用程序时会出现以下错误:
Error:Error converting bytecode to dex:
Cause: Dex cannot parse version 52 byte code.
This is caused by library dependencies that have been compiled using Java 8 or above.
If you are using the 'java' gradle plugin in a library submodule add 
targetCompatibility = '1.7'
sourceCompatibility = '1.7'
to that submodule's build.gradle file.

在添加targetCompatibility = '1.7'和sourceCompatibility = '1.7'后仍然没有任何变化。

非常感谢您的帮助。

1个回答

1

alpn-boot仅适用于标准(桌面/服务器)JVM。它无法与Android设备一起使用。

此外,在这种情况下,引导类路径的更改是运行时选项,而不是编译器参数。

据我所知,对于https请求,它应该在Android上自动工作。

请注意,我也看到了同样的情况,基于okhttp的测试客户端为您的站点获取http/1.1,但为Twitter获取http/2。

$ oksocial --debug -i https://api.twitter.com/robots.txt 2>&1  | grep protocol
11:30:28.849    protocol: h2

$ oksocial --debug -i https://debug.api.heyyka.com/api/v1/dev/err 2>&1 | grep protocol
11:30:45.381    protocol: http/1.1

我认为在你的情况下,你依赖于NPN而不是ALPN,而okhttp不支持。

$ nghttp -v https://debug.api.heyyka.com/api/v1/dev/err
[  0.189] Connected
[  0.362][NPN] server offers:
          * h2
          * http/1.1
The negotiated protocol: h2

我明白了,我应该在服务器端寻找原因,对吗? - Debian
我认为这是最相关的链接 https://www.nginx.com/blog/supporting-http2-google-chrome-users/ - Yuri Schimke
我已经重新编译了nginx,支持openssl-1.0.2+,现在它可以正常运行。在android代码中没有进行任何更改。非常感谢@YuriSchimke! - Debian

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接