开发者

GZIP in Android

开发者 https://www.devze.com 2023-01-29 19:43 出处:网络
i just wanted to ask about sending gzip for post requests using HttpClient in Android? where to get that OutputStr开发者_如何学Goeam to be passed in the GZIPOutputstream?

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

where to get that OutputStr开发者_如何学Goeam to be passed in the GZIPOutputstream?

any snippets?


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


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

精彩评论

暂无评论...
验证码 换一张
取 消