首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Gzip for Android

Gzip for Android
EN

Stack Overflow用户
提问于 2013-02-04 18:13:05
回答 2查看 2.6K关注 0票数 2

我的代码中有以下两个方法。现在,我可以根据以下代码从URL读取JSON。但是,我应该如何修改代码,以便获得gzip文件,然后将其解码为JSON?

代码语言:javascript
复制
private static String readAll(Reader rd) throws IOException {
    StringBuilder sb = new StringBuilder();
    int cp;
    while ((cp = rd.read()) != -1) {
      sb.append((char) cp);
    }
    return sb.toString();
}

public static JSONObject readJsonFromUrl(String url) throws IOException, JSONException {

    InputStream is = new URL(url).openStream();
    try {
      BufferedReader rd = new BufferedReader(new InputStreamReader(is, Charset.forName("UTF-8")));
      String jsonText = readAll(rd);
      Log.e("size of text", jsonText.length()+"");

      JSONObject json;
      if (jsonText.contains("</div>")) {
          json = new JSONObject(jsonText.split("</div>")[1]);
      } else {
          json = new JSONObject(jsonText);
      }
      return json;
    } finally {
      is.close();
    }
}
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2013-02-04 19:07:00

代码语言:javascript
复制
public static JSONObject readJsonFromUrl(String url) throws IOException, JSONException {

    HttpUriRequest request = new HttpGet(url);
    request.addHeader("Accept-Encoding", "gzip");
    HttpClient httpClient = new DefaultHttpClient();
    HttpResponse response = httpClient.execute(request);

    InputStream instream = response.getEntity().getContent();
    org.apache.http.Header contentEncoding = response.getFirstHeader("Content-Encoding");

    JSONObject json = null;
    if (contentEncoding != null && contentEncoding.getValue().equalsIgnoreCase("gzip")) {
        BufferedReader rd = new BufferedReader(new InputStreamReader(new GZIPInputStream(instream)));
        String jsonText = readAll(rd);
          if (jsonText.contains("</div>")) {
              json = new JSONObject(jsonText.split("</div>")[1]);
          } else {
              json = new JSONObject(jsonText);
          }
    }
    return json;
}
票数 3
EN

Stack Overflow用户

发布于 2013-02-04 18:17:04

代码可以使用GZIPInputStream读取GZipped JSON数据。

代码语言:javascript
复制
public static JSONObject readJsonFromUrl(String url) throws IOException, JSONException {

    InputStream is = new URL(url).openStream();
    try {
      //GZIP InputStream
      BufferedReader rd = new BufferedReader(new InputStreamReader(new GZIPInputStream(is), Charset.forName("UTF-8")));
      //Rest of code...
    } finally {
      is.close();
    }
}

Documentation

为了更高效地遍历InputStream,您可以一次读取一行内容。

代码语言:javascript
复制
private static String readAll(BufferedReader rd) throws IOException {
    StringBuilder sb = new StringBuilder();
    String inputLine;

    while ((inputLine = rd.readLine()) != null){
       sb.append(inputLine);
    }

    return sb.toString();
}

完整示例(已测试)

代码语言:javascript
复制
public class StackExample {

    /**
     * @param args
     */
    public static void main(String[] args) {
        try {
            readJsonFromUrl("https://api.stackexchange.com/2.1/answers?order=desc&sort=activity&site=stackoverflow");
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }

    private static String readAll(BufferedReader rd) throws IOException {
           StringBuilder sb = new StringBuilder();
            String inputLine;

            while ((inputLine = rd.readLine()) != null){
               sb.append(inputLine);
            }

            return sb.toString();
    }

    public static void readJsonFromUrl(String url) throws IOException{

        InputStream is = new URL(url).openStream();
        try {
          BufferedReader rd = new BufferedReader(new InputStreamReader(new GZIPInputStream(is), Charset.forName("UTF-8")));
          String jsonText = readAll(rd);
          System.out.println(jsonText);
        } finally {
          is.close();
        }
    }

}
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/14684534

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档