Reputation: 225
I have the following 2 methods in my codes. For now, i could read JSON from URL based on the following codes. However, how should i modify the codes so that I could get a gzip file, then decode it to 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();
}
}
Upvotes: 2
Views: 2877
Reputation: 225
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;
}
Upvotes: 3
Reputation: 94439
The code can use a GZIPInputStream
to read GZipped JSON data.
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();
}
}
To iterate through the InputStream
more efficiently you could read a whole line at a time.
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();
}
}
}
Upvotes: 2