Automatically handles gzip http responses in Android

Link: http://hc.apache.org/httpcomponents-client-ga/tutorial/html/httpagent.html#d4e1261

This page says the following code will install HttpClient to automatically handle gzip responses (transparent to the HttpClient user):

 DefaultHttpClient httpclient = new DefaultHttpClient(); httpclient.addRequestInterceptor(new RequestAcceptEncoding()); httpclient.addResponseInterceptor(new ResponseContentEncoding()); 

However, I cannot find the RequestAcceptEncoding and ResponseContentEncoding classes in the Android SDK. They just went missing - do I need to write them myself?

+6
source share
2 answers

Here is the code I'm using:

  mHttpClient.addResponseInterceptor(new HttpResponseInterceptor() { public void process(final HttpResponse response, final HttpContext context) throws HttpException, IOException { HttpEntity entity = response.getEntity(); Header encheader = entity.getContentEncoding(); if (encheader != null) { HeaderElement[] codecs = encheader.getElements(); for (int i = 0; i < codecs.length; i++) { if (codecs[i].getName().equalsIgnoreCase("gzip")) { response.setEntity(new GzipDecompressingEntity( entity)); return; } } } } }); 

You can also look at SyncService.java from the Google I / O application.

+11
source

Android comes with a rather old version of the Apache HTTP Client library, which does not have classes that you are missing.

You can link the newer version of the Apache HTTP client library with your application (see this answer) or use the AndroidHttpClient , which was introduced in API level 8.

0
source

Source: https://habr.com/ru/post/895535/


All Articles