Gzip on Android

I just wanted to ask about sending gzip for mail requests using HttpClient in Android?

Where can I pass this OutputStream to GZIPOutputstream?

any fragments?

+3
source share
2 answers

Hi, UseHttpUriRequest as below

 String urlval=" http"//www.sampleurl.com/";
    HttpUriRequest req = new HttpGet(urlval);
    req.addHeader("Accept-Encoding", "gzip");
    httpClient.execute(req);

and then Check the response to encode the content as shown below:

InputStream is = response.getEntity().getContent();
Header contentEncoding = response.getFirstHeader("Content-Encoding");
if (contentEncoding != null && contentEncoding.getValue().equalsIgnoreCase("gzip")) {
    is = new GZIPInputStream(is);
}
+7
source

If your data is not too large, you can do it like this:

HttpClient httpClient = new DefaultHttpClient();
HttpPost httpost = new HttpPost(POST_URL);

ByteArrayOutputStream baos = new ByteArrayOutputStream();
GZIPOutputStream gos = new GZIPOutputStream(baos);
gos.write(data.getBytes());
gos.close();
ByteArrayEntity byteArrayEntity = new ByteArrayEntity(baos.toByteArray());
httpost.setEntity(byteArrayEntity);
+2
source

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


All Articles