【问题标题】:Gzip decompressing returns random charactersGzip解压返回随机字符
【发布时间】:2012-12-13 18:37:58
【问题描述】:

我正在尝试从服务器下载 gzip 压缩的 XML 文件,为此我使用以下代码:

        HttpParams httpParameters = new BasicHttpParams();
        int timeoutConnection = 3000;
        HttpConnectionParams.setConnectionTimeout(httpParameters, timeoutConnection);
        int timeoutSocket = 5000;
        HttpConnectionParams.setSoTimeout(httpParameters, timeoutSocket);

        DefaultHttpClient client = new DefaultHttpClient(httpParameters);
        HttpGet response = new HttpGet(urlData);

        client.addRequestInterceptor(new HttpRequestInterceptor() {
            @Override
            public void process(HttpRequest request, HttpContext context) {
                // Add header to accept gzip content
                if (!request.containsHeader("Accept-Encoding")) {
                    request.addHeader("Accept-Encoding", "gzip");
                }
            }
        });

        client.addResponseInterceptor(new HttpResponseInterceptor() {
            @Override
            public void process(HttpResponse response, HttpContext context) {
                // Inflate any responses compressed with gzip
                final HttpEntity entity = response.getEntity();
                final Header encoding = entity.getContentEncoding();
                if (encoding != null) {
                    for (HeaderElement element : encoding.getElements()) {
                        if (element.getName().equalsIgnoreCase("gzip")) {
                            response.setEntity(new InflatingEntity(response.getEntity()));
                            break;
                        }
                    }
                }
            }

        });    

        ResponseHandler<String> responseHandler = new BasicResponseHandler();

        return client.execute(response, responseHandler);

InflatingEntity 方法:

private static class InflatingEntity extends HttpEntityWrapper {
        public InflatingEntity(HttpEntity wrapped) {
            super(wrapped);
        }

        @Override
        public InputStream getContent() throws IOException {
            return new GZIPInputStream(wrappedEntity.getContent());
        }

        @Override
        public long getContentLength() {
            return -1;
        }
    }

如果我删除与 Gzip 压缩相关的所有内容,并用普通 XML 替换服务器中的压缩 XML 文件,一切正常,但在我实施 Gzip 压缩后,我得到了压缩字符串:

有谁知道我的代码中缺少什么来获取解压缩的 XML?

【问题讨论】:

  • 检查 xml 是否没有在服务器上压缩两次...代码看起来不错,应该可以工作...
  • 看起来像一个带有嵌入文件名的压缩数据流。确保在压缩源数据时确实发生了 gzip 膨胀。
  • @fadden 是的,你是对的,它没有解压缩响应,这是因为这一行:final HttpEntity entity = response.getEntity();是空的,有什么想法吗?

标签: java android gzip apache-commons-httpclient


【解决方案1】:

我已经解决了这个问题,我的响应没有实体所以代码没有解压响应,因为那部分代码没有到达,这里是 responseinterceptor 中的修改:

   client.addResponseInterceptor(new HttpResponseInterceptor() {
                    @Override
                    public void process(HttpResponse response, HttpContext context) {
                            response.setEntity(new InflatingEntity(response.getEntity()));

                    }

                });    

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-02-06
    • 1970-01-01
    • 2017-10-10
    • 1970-01-01
    • 1970-01-01
    • 2020-04-29
    • 1970-01-01
    相关资源
    最近更新 更多