2

コードには次の2つのメソッドがあります。今のところ、次のコードに基づいてURLからJSONを読み取ることができます。ただし、gzipファイルを取得してJSONにデコードできるようにコードを変更するにはどうすればよいですか?

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();
    }
}
4

2 に答える 2

3
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;
}
于 2013-02-04T11:07:00.437 に答える
2

コードはを使用しGZIPInputStreamてGZipされたJSONデータを読み取ることができます。

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();
    }
}

ドキュメンテーション

より効率的に繰り返すためInputStreamに、一度に1行全体を読み取ることができます。

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 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();
        }
    }

}
于 2013-02-04T10:17:03.550 に答える