2

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

我在我的活动中调用了我的 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)

请帮我解决这个错误。

4

0 回答 0