Android:HTTP 通信应使用“Accept-Encoding: gzip”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1573391/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Android: HTTP communication should use "Accept-Encoding: gzip"
提问by znq
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.
我有一个与请求 JSON 数据的网络服务器的 HTTP 通信。我想用Content-Encoding: gzip
. 有没有办法Accept-Encoding: gzip
在我的 HttpClient 中设置?gzip
Android References 中的搜索未显示任何与 HTTP 相关的内容,如您在此处看到的。
回答by Bakhtiyor
You should use http headers to indicate a connection can accept gzip encoded data, e.g:
您应该使用 http 标头来指示连接可以接受 gzip 编码的数据,例如:
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);
}
回答by Volker Neumann
If you're using API level 8 or above there's AndroidHttpClient.
如果您使用 API 级别 8 或更高级别,则有AndroidHttpClient。
It has helper methods like:
它具有辅助方法,例如:
public static InputStream getUngzippedContent (HttpEntity entity)
and
和
public static void modifyRequestToAcceptGzipResponse (HttpRequest request)
leading to much more succinct code:
导致更简洁的代码:
AndroidHttpClient.modifyRequestToAcceptGzipResponse( request );
HttpResponse response = client.execute( request );
InputStream inputStream = AndroidHttpClient.getUngzippedContent( response.getEntity() );
回答by Niqo
I think the sample of code at this link is more interesting: ClientGZipContentCompression.java
我认为这个链接的代码示例更有趣: ClientGZipContentCompression.java
They are using HttpRequestInterceptorand HttpResponseInterceptor
他们正在使用HttpRequestInterceptor和HttpResponseInterceptor
Sample for request:
索取样品:
httpclient.addRequestInterceptor(new HttpRequestInterceptor() {
public void process(
final HttpRequest request,
final HttpContext context) throws HttpException, IOException {
if (!request.containsHeader("Accept-Encoding")) {
request.addHeader("Accept-Encoding", "gzip");
}
}
});
Sample for answer:
回答样本:
httpclient.addResponseInterceptor(new HttpResponseInterceptor() {
public void process(
final HttpResponse response,
final HttpContext context) throws HttpException, IOException {
HttpEntity entity = response.getEntity();
Header ceheader = entity.getContentEncoding();
if (ceheader != null) {
HeaderElement[] codecs = ceheader.getElements();
for (int i = 0; i < codecs.length; i++) {
if (codecs[i].getName().equalsIgnoreCase("gzip")) {
response.setEntity(
new GzipDecompressingEntity(response.getEntity()));
return;
}
}
}
}
});
回答by Dimitar Dimitrov
I haven't used GZip, but I would assume that you should use the input stream from your HttpURLConnection
or HttpResponse
as GZIPInputStream
, and not some specific other class.
我没有使用过 GZip,但我假设您应该使用来自您的HttpURLConnection
或HttpResponse
as的输入流GZIPInputStream
,而不是某个特定的其他类。
回答by kospol
In my case it was like this:
就我而言,它是这样的:
URLConnection conn = ...;
InputStream instream = conn.getInputStream();
String encodingHeader = conn.getHeaderField("Content-Encoding");
if (encodingHeader != null && encodingHeader.toLowerCase().contains("gzip"))
{
instream = new GZIPInputStream(instream);
}