我如何阅读来自服务器的gzip作为http响应

时间:2014-08-07 11:28:26

标签: android http gzip

您正在开发Android应用。我从gZip格式的服务器得到响应如何读取这个gzip inputStream。我发布下面的代码请告诉我任何错误。谢谢提前。

  HttpConnectionParams.setConnectionTimeout(LaunchActivity.client.getParams(),  
        HttpConnectionParams.setSoTimeout(LaunchActivity.client.getParams(), 10000);

        HttpGet post = new HttpGet(localUrl);

        HttpResponse response = LaunchActivity.client.execute(post);
        post.setHeader("Accept", "application/json");
        post.setHeader("Content-type", "application/json");
        post.setHeader("Accept-Encoding", "gzip");


        if (response.getStatusLine().getStatusCode() >= 200 ||response.getStatusLine().getStatusCode() < 300)
        {
            //is = response.getEntity().getContent();
            System.out.println("this is input stream from server====== "+is);

             HttpEntity entity = response.getEntity();
              if (entity != null) {
                is = entity.getContent();
                Header contentEncoding = response.getFirstHeader("Content-Encoding");
                if (contentEncoding != null && contentEncoding.getValue().equalsIgnoreCase("gzip")) {
                    is = new GZIPInputStream(is);
                }
                String resultString = convertStreamToString(is);
                System.out.println("resultString========   "+resultString);
                is.close();
                String rst = decompresss(resultString);
                System.out.println("rst==== "+rst);

               // return resultString;
              }

  I got respose as below format i.e
 x��X�o�6�W=�(R����nkQ����Q<ZJl�#)gZ��}g%v[��t�`X�N���}w<�c���G�Y��w�]|��

0 个答案:

没有答案