Mikey
Mikey

Reputation: 4238

GZIP in Android

i just wanted to ask about sending gzip for post requests using HttpClient in Android?

where to get that OutputStream to be passed in the GZIPOutputstream?

any snippets?

Upvotes: 3

Views: 3602

Answers (2)

justin
justin

Reputation: 21

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);

Upvotes: 2

Sankar Ganesh PMP
Sankar Ganesh PMP

Reputation: 12027

Hi UseHttpUriRequest as shown below

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

and then Check response for content encoding 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);
}

Upvotes: 6

Related Questions