コードには次の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();
}
}