Thank you for Aksenov Vladimir`s reply. Your answer saved me a lot of time. Everything is working fine after I upgraded okhttp from 3.x to 4.11.
Here are some additional details:
- When users explicitly include the "Accept-Encoding: gzip" header, they need to handle the decompression of the response content themselves.
- When users do not explicitly specify "Accept-Encoding" and "Range" okhttp will automatically add "Accept-Encoding: gzip" to the request header, and automatically decompress the response content (if "Content-Encoding" is gzip).
The relevant code is as follows:okhttp3.internal.http.BridgeInterceptor
// If we add an "Accept-Encoding: gzip" header field we're responsible for also decompressing // the transfer stream. var transparentGzip = false if (userRequest.header("Accept-Encoding") == null && userRequest.header("Range") == null) { transparentGzip = true requestBuilder.header("Accept-Encoding", "gzip") }if (transparentGzip &&"gzip".equals(networkResponse.header("Content-Encoding"), ignoreCase = true) && networkResponse.promisesBody()) { val responseBody = networkResponse.body if (responseBody != null) { val gzipSource = GzipSource(responseBody.source()) val strippedHeaders = networkResponse.headers.newBuilder() .removeAll("Content-Encoding") .removeAll("Content-Length") .build() responseBuilder.headers(strippedHeaders) val contentType = networkResponse.header("Content-Type") responseBuilder.body(RealResponseBody(contentType, -1L, gzipSource.buffer())) } }