1

我想知道在 gzip 编码响应的情况下我应该做什么。这是处理我的回复的方法:

private InputStream getResultStream(Response response) throws IOException
{
    InputStream resultStream = null;
    if(response != null)
    {
        String encoding = response.getHeader("Content-Encoding");
        if((encoding != null) && (encoding.equalsIgnoreCase("gzip")))
        {
            // What to do here ?
            Log.d("Stream :", "Read GZIP");

        } else if ((encoding != null) && encoding.equalsIgnoreCase("deflate")) {
            resultStream = new InflaterInputStream(response.getStream(), new Inflater(true));
            Log.d("Stream :", "Read Deflated.");
        } else {
            resultStream = response.getStream();
            Log.d("Stream :","Read Normal.");
        }       
    }

    return resultStream;
}

我该如何处理?

4

4 回答 4

2

将您的流包装在 a 中GZIPInputStream并从中读取。

resultStream = new GZIPInputStream(resultStream);
//proceed reading as usual
于 2012-06-25T12:46:35.883 回答
0

如果您只想知道如何读取压缩流,只需使用GZIPInputStream包装您的输入流

 InputStream is = ...
 GZIPInputStream zis = new GZIPInputStream(new BufferedInputStream(is));
 try {
     // Reading from 'zis' gets you the uncompressed bytes...
     processStream(zis);
 } finally {
     zis.close();
 }
于 2012-06-25T12:48:30.957 回答
0

请执行下列操作 :

resultStream = new java.util.zip.GZIPInputStream(response.getStream()); 
于 2012-06-25T12:48:33.443 回答
0

免责声明:我没有机会对此进行测试。

根据Android 的 HTTP 客户端博客文章所说,如果您使用的是 Android 2.3+,那么HttpURLConnection可以自动为您完成。

于 2012-06-25T12:52:39.593 回答