安卓:HTTP通讯应使用“接受编码:gzip" [英] Android: HTTP communication should use "Accept-Encoding: gzip"

查看:100
本文介绍了安卓:HTTP通讯应使用“接受编码:gzip"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个HTTP通信的网络服务器请求JSON数据。我想COM preSS与此数据流内容编码:gzip 。有没有一种方法我可以设置接受编码:gzip 在我的HttpClient的?在Android引用的搜索 GZIP 不显示任何有关HTTP,正如你所看到的这里

I've a HTTP communication to a webserver requesting JSON data. I'd like compress this data stream with Content-Encoding: gzip. Is there a way I can set Accept-Encoding: gzip in my HttpClient? The search for gzip in the Android References doesn't show up anything related to HTTP, as you can see here.

推荐答案

您应该使用HTTP头指示连接可以接受gzip压缩连接codeD数据,如:

You should use http headers to indicate a connection can accept gzip encoded data, e.g:

HttpUriRequest request = new HttpGet(url);
request.addHeader("Accept-Encoding", "gzip");
// ...
httpClient.execute(request);

检查响应内容编码:

Check response for content encoding:

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

这篇关于安卓:HTTP通讯应使用“接受编码:gzip"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆