http://docs.blackberry.com/sampledata.json
This is my web service and I want to parse and retrieve vehicleType, vehicleColor, fuel, name, experiencePoints, treadType
The given url returns a JSONArray, so the method getJSONFromUrl should return JSONArray..
try {
HttpParams params = new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(params, Constants.CONNECTION_TIME_OUT);
HttpConnectionParams.setSoTimeout(params, 0);
HttpClient httpClient = new DefaultHttpClient(params);
//prepare the HTTP GET call
HttpGet httpget = new HttpGet(urlString);
//get the response entity
HttpEntity entity = httpClient.execute(httpget).getEntity();
if (entity != null) {
//get the response content as a string
String response = EntityUtils.toString(entity);
//consume the entity
entity.consumeContent();
// When HttpClient instance is no longer needed, shut down the connection manager to ensure immediate deallocation of all system resources
httpClient.getConnectionManager().shutdown();
//return the JSON response
JSONArray array = new JSONArray(response.trim());
for(int i = 0 ; i < array.length() ; i++) { //get the current JSON object
JSONObject object = (JSONObject) array.get(i);
String vehicleType = object.getString("vehicleType");
......
}
}
}catch (Exception e) {
e.printStackTrace();
}