androidokhttphttp3

Can't use HTTP/3 with CronetTransport in Android


I'm trying to implement HTTP/3 with QUIC support in my Android app using OkHttp and the Cronet transport. However, all requests are being made under HTTP/2. I have used different devices with different versions. I've tried with Retrofit as well and same result.

The server I'm reaching out does support HTTP3 I've tested it on web and it works fine.

private val engine = CronetEngine.Builder(MainActivity.appContext).enableHttpCache(CronetEngine.Builder.HTTP_CACHE_DISK, 10 * 1024 * 1024)
    .build()


    private val callFactory = OkHttpClient.Builder()
        .addInterceptor(CronetInterceptor.newBuilder(engine).build())
    .build();

private fun getRequest(url: String): JSONObject? {
        val request: Request = Request.Builder()
            .url(url)
            .build()


        val response = callFactory.newCall(request).execute()
        return response.body?.string()?.let { JSONObject(it) }
    }

enter image description here


Solution

  • Seems like OkHttp does not enable QUIC/HTTP3 by default. So I had to add it manually on its builder.

    val callFactory =  OkHttpClient.Builder()
       .protocols(listOf(Protocol.QUIC, Protocol.HTTP_2))
       .addInterceptor(CronetInterceptor.newBuilder(engine).build())
       .build();
    

    https://github.com/google/cronet-transport-for-okhttp/pull/23