gpt4 book ai didi

android - 在 Android 中自动处理 gzip http 响应

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:38:28 26 4
gpt4 key购买 nike

引用:http://hc.apache.org/httpcomponents-client-ga/tutorial/html/httpagent.html#d4e1261

此页面说明以下代码将设置 HttpClient 以自动处理 gzip 响应(对 HttpClient 的用户透明):

DefaultHttpClient httpclient = new DefaultHttpClient();
httpclient.addRequestInterceptor(new RequestAcceptEncoding());
httpclient.addResponseInterceptor(new ResponseContentEncoding());

但是,我在 Android SDK 中找不到 RequestAcceptEncodingResponseContentEncoding 类。它们只是丢失了——我需要自己写这些吗?

最佳答案

这是我使用的代码:

   mHttpClient.addResponseInterceptor(new HttpResponseInterceptor() {
public void process(final HttpResponse response,
final HttpContext context) throws HttpException,
IOException {
HttpEntity entity = response.getEntity();
Header encheader = entity.getContentEncoding();
if (encheader != null) {
HeaderElement[] codecs = encheader.getElements();
for (int i = 0; i < codecs.length; i++) {
if (codecs[i].getName().equalsIgnoreCase("gzip")) {
response.setEntity(new GzipDecompressingEntity(
entity));
return;
}
}
}
}
});

您可能还想看看 SyncService.java来自 Google I/O 应用程序。

关于android - 在 Android 中自动处理 gzip http 响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7139268/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com