I'm using Retrofit
to manage my requests and want to make some tests to check de request size using or not using gzip.
By default does OkHttp
performs gzip compression to requests or it must be implemented with an interceptor?
I've added
@Headers({
"Accept-Encoding: gzip, deflate",
"Content-Encoding: gzip"
})
or:
@Headers({
"Content-Type: application/json;charset=utf-8",
"Accept: application/json"
})
to my requests and did not see any change on the request length.
OkHttp will do transparent gzip on response bodies unless you disable the feature with this header:
Accept-Encoding: identity
We can use this code
OkHttpClient client = new OkHttpClient();
Request request = new Request.Builder().url(url)
.addHeader("X-TOKEN", "Bearer " + Auth.getInstance(mContext).getToken())
.addHeader("Accept-Encoding", "gzip")
.build();
Response response = client.newCall(request).execute();
if (responseCode == 200) {
// Regular JSON parsing to model
ItemModel itemModel = LoganSquare.parse(response.body().byteStream(), ItemModel.class);
long responseSize = response.body().contentLength();
// Manually decompress GZIP?
ItemModel itemModel = LoganSquare.parse(new GZIPInputStream(response.body().byteStream()), ItemModel.class);
long responseSize = response.body().contentLength();
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With