L'ho capito. L'idea è di aggiungere un interceptor personalizzato che prenderà la risposta non ancora decompressa e decomprimerla 'manualmente' - fare la stessa cosa che OkHttp farebbe automaticamente in base all'intestazione Content-Encoding, ma senza richiedere quell'intestazione.
è come DIS:
OkHttpClient.Builder clientBuilder = new OkHttpClient.Builder()
.addInterceptor(new UnzippingInterceptor());
OkHttpClient client = clientBuilder.build();
E l'Interceptor è come DIS:
private class UnzippingInterceptor implements Interceptor {
@Override
public Response intercept(Chain chain) throws IOException {
Response response = chain.proceed(chain.request());
return unzip(response);
}
}
e la funzione di decompressione è come DIS:
// copied from okhttp3.internal.http.HttpEngine (because is private)
private Response unzip(final Response response) throws IOException {
if (response.body() == null) {
return response;
}
GzipSource responseBody = new GzipSource(response.body().source());
Headers strippedHeaders = response.headers().newBuilder()
.removeAll("Content-Encoding")
.removeAll("Content-Length")
.build();
return response.newBuilder()
.headers(strippedHeaders)
.body(new RealResponseBody(strippedHeaders, Okio.buffer(responseBody)))
.build();
}
Interessante. Usa un intercettore. Buona idea. Sembra che dovrebbe funzionare. – itsymbal
@itsymbal lo fa, lo uso –