http2 似乎不适用于 OkHttp3 和 retrofit2

http2 doesn't seem to be working with OkHttp3 and retrofit2

我刚刚从 okhttp2 + retrofit 升级到 okhttp3 + retrofit2 但无法让 http2 在 Android 客户端上运行。

我的服务器是 运行 nginx 1.14.0,启用了 http2。 (iOS 客户端在 http2 上运行良好)

这是创建 okhttp 客户端的代码

    private static OkHttpClient createOkHttpClient(Application app,
                                               NetworkInterceptor networkInterceptor,
                                               HttpLoggingInterceptor httpLoggingInterceptor) {
    // Install an HTTP cache in the application cache directory.
    File  cacheDir = new File(app.getCacheDir(), "http");
    Cache cache    = new Cache(cacheDir, DISK_CACHE_SIZE);

    Security.insertProviderAt(
            new org.conscrypt.OpenSSLProvider(), 1);

    OkHttpClient.Builder okHttpClientBuilder = new OkHttpClient().newBuilder()
            .cache(cache)
            .connectTimeout(15, TimeUnit.SECONDS)
            .readTimeout(15, TimeUnit.SECONDS)
            .writeTimeout(15, TimeUnit.SECONDS)
            .callTimeout(30, TimeUnit.SECONDS)
            .protocols(Arrays.asList(Protocol.HTTP_2, Protocol.HTTP_1_1))
            .followRedirects(true)
            .followSslRedirects(true)
            .addInterceptor(networkInterceptor)
            .addInterceptor(httpLoggingInterceptor);

    if (BuildConfig.DEBUG) {
        okHttpClientBuilder.addNetworkInterceptor(new StethoInterceptor());
    }
    return okHttpClientBuilder.build();
}

我正在使用匕首 2 创建 Api 和改造 2。

我的测试设备是运行 Android 8.1.

我已经阅读了文档,这个设置应该是 运行 http2。我的实现有问题吗?

你的服务器是 HTTPS 吗? OkHttp 需要 HTTP/2.

的 HTTPS

我在重建 nginx 后让它工作了。客户端代码没问题

ubuntu 14.04 附带的默认 nginx 不支持 google 客户端开箱即用的 http2。它使用 NPN 而不是 ALPN。为了支持 APLN,我们需要重建 opensslnginx.

用 ALPN 支持重建 openssl 包(OpenSSL 1.0.2h 2016 年 5 月 3 日)

$ wget -c https://www.openssl.org/source/openssl-1.0.2h.tar.gz
$ tar xf openssl-1.0.2h.tar.gz -C /usr/local/
$ cd /usr/local/openssl-1.0.2h
$ ./config
$ make depend
$ make
$ make test
$ make install
$ mv /usr/bin/openssl /usr/bin/openssl_1.0.1e
$ ln -s /usr/local/ssl/bin/openssl /usr/bin/openssl
$ openssl version
OpenSSL 1.0.2h  3 May 2016

使用新的 openssl 重建 nginx

$ sudo apt-get install libpcre3-dev libssl-dev libxml2-dev libxslt-dev libgd-dev libgeoip-dev
$ wget http://nginx.org/download/nginx-1.14.2.tar.gz
$ tar -xvzf nginx-1.14.2.tar.gz 
$ cd nginx-1.14.2/
$ ./configure --prefix=/usr/share/nginx --sbin-path=/usr/sbin/nginx --conf-path=/etc/nginx/nginx.conf --lock-path=/var/lock/nginx.lock --pid-path=/run/nginx.pid --http-fastcgi-temp-path=/var/lib/nginx/fastcgi --with-debug --with-pcre-jit --with-ipv6 --with-http_ssl_module --with-http_stub_status_module --with-http_realip_module --with-http_auth_request_module --with-http_addition_module --with-http_dav_module --with-http_geoip_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_image_filter_module --with-http_v2_module --with-http_sub_module --with-http_xslt_module --with-stream --with-stream_ssl_module --with-mail --with-mail_ssl_module --with-threads --with-openssl=/usr/local/openssl-1.0.2h/
$ sudo make
$ sudo make install
$ nginx -V
nginx version: nginx/1.14.2
built by gcc 4.8.4 (Ubuntu 4.8.4-2ubuntu1~14.04.4)
built with OpenSSL 1.0.2h  3 May 2016
TLS SNI support enabled

之后重启 nginx 就可以正常工作了。