I spent many hours with making HTTP/2 support on OkHttp3 to work and I ran out of ideas.
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 ..."
Server with nginx/1.10.2
and https
JAVA_VERSION="1.8.0_76"
Android version="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();
I found that I need to add the Jetty ALPN jar to my bootclasspath. So I've just added following lines in my 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.
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.
In version 8* I have following error while building the app:
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.
Thank you in advance for any help
alpn-boot is only used for a standard (Desktop/Server) JVM. It will not work with an Android device.
Also the change to bootclasspath is a runtime option in that case, not a compiler argument.
AFAIK it should work automatically on Android for https requests.
n.b. 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.
$ 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.
$ 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