【发布时间】:2017-06-05 11:00:56
【问题描述】:
我花了很多时间在 OkHttp3 上实现 HTTP/2 支持,但我没有想法。
如何检查它是否有效:
在 access.log 中检查哪个协议使用连接到服务器的客户端:
浏览器:"GET /favicon.ico HTTP/2.0" 200 382 https://server.com "" Mozilla / 5.0 (X11; Linux x86_64)"
安卓客户端:'GET /api/v2/ ... HTTP/1.1 "200 3717" - "" Android ..."
我用什么:
带有nginx/1.10.2 和https 的服务器
JAVA_VERSION="1.8.0_76"
安卓版本="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')
}
}
我尝试了来自 https://mvnrepository.com/artifact/org.mortbay.jetty.alpn/alpn-boot 的所有 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' 仍然没有。
提前感谢您的帮助
【问题讨论】:
标签: java android retrofit2 okhttp3 http2