我与请求JSON数据的‘ve服务器进行了HTTP通信。我想用Content-Encoding: gzip
压缩这个数据流。有没有办法在我的HttpClient中设置Accept-Encoding: gzip
?在Android参考资料中搜索gzip
并没有显示任何与HTTP相关的内容,正如您所看到的here。
发布于 2009-10-16 06:53:18
您应该使用http标头来指示连接可以接受gzip编码的数据,例如:
HttpUriRequest request = new HttpGet(url);
request.addHeader("Accept-Encoding", "gzip");
// ...
httpClient.execute(request);
检查内容编码的响应:
InputStream instream = response.getEntity().getContent();
Header contentEncoding = response.getFirstHeader("Content-Encoding");
if (contentEncoding != null && contentEncoding.getValue().equalsIgnoreCase("gzip")) {
instream = new GZIPInputStream(instream);
}
发布于 2012-02-09 02:10:53
如果你使用的是8级或更高级别的API8,那就是AndroidHttpClient。
它有一些辅助方法,比如:
public static InputStream getUngzippedContent (HttpEntity entity)
和
public static void modifyRequestToAcceptGzipResponse (HttpRequest request)
导致更简洁的代码:
AndroidHttpClient.modifyRequestToAcceptGzipResponse( request );
HttpResponse response = client.execute( request );
InputStream inputStream = AndroidHttpClient.getUngzippedContent( response.getEntity() );
发布于 2009-10-15 18:44:53
我没有使用GZip,但我假设您应该使用来自HttpURLConnection
或HttpResponse
的输入流作为GZIPInputStream
,而不是某个特定的其他类。
https://stackoverflow.com/questions/1573391
复制相似问题