【问题标题】:http2 doesn't seem to be working with OkHttp3 and retrofit2http2 似乎不适用于 OkHttp3 和 retrofit2
【发布时间】:2019-05-11 13:39:41
【问题描述】:

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

我的服务器正在运行启用了 http2 的 nginx 1.14.0。 (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();
}

我正在使用 dagger 2 来创建带有改造 2 的 Api。

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

我已阅读文档,此设置应该运行 http2。我的实现有问题吗?

【问题讨论】:

    标签: android nginx retrofit okhttp alpn


    【解决方案1】:

    你的服务器是 HTTPS 吗? OkHttp 对于 HTTP/2 需要 HTTPS。

    【讨论】:

    • 是的,它是一个 https 服务器,我正在尝试调用 https 端点。
    【解决方案2】:

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

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

    重建带有 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,它应该可以正常工作。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-06-05
      • 2020-04-04
      • 2023-04-06
      • 2017-08-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多