如何在android中使用gzip格式解压缩我的json数据?

时间:2016-01-28 10:07:30

标签: android gunzip

我想解压缩json数据并存储到string中。我尝试了更多,但效果不佳。

我将我的jsonparser类调用到我的活动中,当我运行我的应用程序时,它引发了一个错误。

这是我的JSONParse.java

public class JSONParse {
    static String res=null;
    public final static int GET = 1;
    public final static int POST = 2;
    InputStreamReader reader = null;
    ByteArrayInputStream bais=null;
    GZIPInputStream gzis=null;
    BufferedReader in=null;
    String str="";
    String outstr="";
    public JSONParser() {

    }

    public String makeServiceCall(String url, int method,
                                  List<NameValuePair> params) {
        try {
            DefaultHttpClient httpClient = new DefaultHttpClient();
            HttpEntity httpEntity = null;
            HttpResponse httpResponse = null;

            if (method == POST) {
                HttpPost httpPost = new HttpPost(url);
                if (params != null) {
                    httpPost.setEntity(new UrlEncodedFormEntity(params));
                }
                httpResponse = httpClient.execute(httpPost);

            } else if (method == GET) {
                if (params != null) {
                    String paramString = URLEncodedUtils
                            .format(params, "utf-8");
                    url += "?" + paramString;
                }

                HttpGet httpGet = new HttpGet(url);
                httpResponse = httpClient.execute(httpGet);
            }


            httpEntity = httpResponse.getEntity();
            res=EntityUtils.toString(httpEntity);


            bais=new ByteArrayInputStream(res.getBytes("UTF-8"));
            gzis=new GZIPInputStream(bais);
            reader=new InputStreamReader(gzis,"UTF-8");
            BufferedReader in = new BufferedReader(reader);

            StringBuilder sb = new StringBuilder();
            String line = null;
            outstr="";
            while ((line = in.readLine()) != null) {

                outstr+=line;
            }
            Log.e("outSTR",""+outstr);
            reader.close();


        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return outstr;
    }

}

这是我的错误

W/System.err: java.io.IOException: unknown format (magic number ef1f)
at java.util.zip.GZIPInputStream.<init>(GZIPInputStream.java:109)
at java.util.zip.GZIPInputStream.<init>(GZIPInputStream.java:88)
at comman.ServiceHandler.makeServiceCall(ServiceHandler.java:77)

请帮我解决这个错误。

0 个答案:

没有答案