Since I've looked far and wide for a good example of this to no avail, I have created my own using the JSONArray
and JSONObject
classes. It took me a while to realize that the push
method for JSONObject
overwrites everything previously pushed.
public static JSONArray getJSON(String url) throws IOException, JSONException, URISyntaxException, TransformerException{
HttpClient httpClient = new DefaultHttpClient();
JSONArray jArray = new JSONArray();
Object[] q = null;
Deque<String> queue = new ArrayDeque<String>();
try{
HttpGet httpGet = new HttpGet(url);
HttpResponse response = httpClient.execute(httpGet);
int responseCode = response.getStatusLine().getStatusCode();
logger.info("Response Code : " + responseCode);
if (responseCode != 404){
logger.info("Response" + response.getEntity().getContent());
try(BufferedReader reader = new BufferedReader(
new InputStreamReader(
response.getEntity().getContent()))) {
if(reader != null){
String aux = "";
while ((aux = reader.readLine()) != null) {
queue.add(aux);
}
q = queue.toArray();
for(int i = 0; i < q.length; i++){
String[] row = q[i].toString().split(",");
JSONObject json = new JSONObject();
json.put("col0", row[0]);
json.put("col1", row[1]);
json.put("col2", row[2]);
json.put("col3", row[3]);
json.put("col4", row[4]);
json.put("col5", row[5]);
jArray.put(json);
}
}
}
}
}
return null;
}
return jArray;
}finally{
httpClient.getConnectionManager().shutdown();
}
}