简体   繁体   English

带有OkHttp3和Retrofit2的HTTP / 2

[英]HTTP/2 with OkHttp3 and Retrofit2

I spent many hours with making HTTP/2 support on OkHttp3 to work and I ran out of ideas. 我花了很多时间在OkHttp3上使用HTTP / 2支持工作,我的想法用尽了。

How do I check if it works: 我如何检查它是否有效:

Checking in access.log which protocol uses a client that connects to the server: Browser: "GET /favicon.ico HTTP/2.0" 200 382 https://server.com "" Mozilla / 5.0 (X11; Linux x86_64)" Android client: 'GET /api/v2/ ... HTTP/1.1 "200 3717" - "" Android ..." 检查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 ..."

What I use: 我用的是什么:

Server with nginx/1.10.2 and https JAVA_VERSION=" 1.8.0_76 " Android version=" 6.0.1 " 服务器使用nginx/1.10.2https JAVA_VERSION =“ 1.8.0_76 ”Android版本=“ 6.0.1

build.gradle 的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'
...

How I use: 我怎么用

 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();

What I tried 我尝试了什么

I found that I need to add the Jetty ALPN jar to my bootclasspath. 我发现我需要将Jetty ALPN jar添加到我的bootclasspath中。 So I've just added following lines in my build.gradle(Project): 所以我刚刚在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')
        }
}

I tried with all alpn-boot versions from : https://mvnrepository.com/artifact/org.mortbay.jetty.alpn/alpn-boot but nothing, even error messege. 我尝试了所有alpn-boot版本: https ://mvnrepository.com/artifact/org.mortbay.jetty.alpn/alpn-boot但没有,甚至是错误信息。

I also tried to add in build.gradle compile("org.mortbay.jetty.alpn:alpn-boot:VERSION_NUMBER") , but with version 7* still have no errors and doesn't work. 我还尝试添加build.gradle compile("org.mortbay.jetty.alpn:alpn-boot:VERSION_NUMBER") ,但版本7 *仍然没有错误并且不起作用。 In version 8* I have following error while building the app: 在版本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.

Adding targetCompatibility = '1.7' with sourceCompatibility = '1.7' still nothing. 使用sourceCompatibility ='1.7'添加targetCompatibility ='1.7'仍然没有。

Thank you in advance for any help 预先感谢您的任何帮助

alpn-boot is only used for a standard (Desktop/Server) JVM. alpn-boot仅用于标准(桌面/服务器)JVM。 It will not work with an Android device. 它不适用于Android设备。

Also the change to bootclasspath is a runtime option in that case, not a compiler argument. 此外,对bootclasspath的更改是运行时选项,而不是编译器参数。

AFAIK it should work automatically on Android for https requests. AFAIK它应该在Android上自动为https请求工作。

nb I'm also seeing the same thing, with a test client based on okhttp, getting http/1.1 for your site but http/2 for twitter. 我也看到同样的事情,基于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

I think in your case you are relying on NPN instead of ALPN, which okhttp does not support. 我认为在你的情况下你依赖于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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM