我的代码中有以下两个方法。现在,我可以根据以下代码从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();
}
}发布于 2013-02-04 19:07:00
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-04 18:17:04
代码可以使用GZIPInputStream读取GZipped 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();
}
}Documentation
为了更高效地遍历InputStream,您可以一次读取一行内容。
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();
}
}
}https://stackoverflow.com/questions/14684534
复制相似问题