HTTP/2 与 OkHttp3 和 Retrofit2

Deb*_*ian 5 java android okhttp http2 retrofit2

我花了很多时间在 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 version=" 6.0.1 "

构建.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'
...
Run Code Online (Sandbox Code Playgroud)

我如何使用:

 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();
Run Code Online (Sandbox Code Playgroud)

我尝试过的

我发现我需要将 Jetty ALPN jar 添加到我的 bootclasspath 中。所以我刚刚在我的 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')
        }
}
Run Code Online (Sandbox Code Playgroud)

我尝试了来自以下位置的所有 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.
Run Code Online (Sandbox Code Playgroud)

添加 targetCompatibility = '1.7' 和 sourceCompatibility = '1.7' 仍然没有任何结果。

预先感谢您的任何帮助

Yur*_*mke 1

alpn-boot 仅用于标准(桌面/服务器)JVM。它不适用于 Android 设备。

在这种情况下,对 bootclasspath 的更改也是一个运行时选项,而不是编译器参数。

据我所知,它应该在 Android 上自动处理 https 请求。

注:我也看到了同样的事情,使用基于 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
Run Code Online (Sandbox Code Playgroud)

我认为在你的情况下你依赖的是 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
Run Code Online (Sandbox Code Playgroud)