I have a json returning back to me from server. I am trying to iterate over it but its gving me error on this line
JSONObject obj = jsonHospitals.getJSONObject(i);
The error underlines in red .getJSONObject(i) and gives me two options 1- change to optJSONObject(...) 2- change type of 'i' to 'String'
What can I do to use JSONObject and iterate over the json. I am new to android so please bear with me fellas! Below is some more of the code
String responseData = new String(charArray);
JSONObject jsonHospitals = new JSONObject(responseData);
for(int i = 0; i< jsonHospitals.length(); i++){
JSONObject obj = jsonHospitals.getJSONObject(i);
JSONObject hospital_obj = obj.getJSONObject("Hospital");
String hospital_name = hospital_obj.getString("hospital_name");
Log.v(TAG, hospital_name);
}
Thanks UPDATE *OK TRIED SOMETHING NEW AND ALL POSTED HERE NOW*
I tried this code
try {
URL hospitalFeedUrl = new URL("siteresponseaddress");
HttpURLConnection connection = (HttpURLConnection)hospitalFeedUrl.openConnection();
connection.connect();
responseCode = connection.getResponseCode();
if(responseCode == HttpURLConnection.HTTP_OK){
InputStream inputStream = connection.getInputStream();
Reader reader = new InputStreamReader(inputStream);
int contentLenght = connection.getContentLength();
char[] charArray = new char[contentLenght];
reader.read(charArray);
String responseData = new String(charArray);
JSONArray jsonHospitals = new JSONArray(responseData);
for(int i = 0; i< jsonHospitals.length(); i++){
JSONObject obj = jsonHospitals.getJSONObject(i);
JSONObject hospital_obj = obj.getJSONObject("Hospital");
String hospital_name = hospital_obj.getString("hospital_name");
Log.v(TAG, hospital_name);
}
Log.v(TAG, responseData);
}
else {
Log.i(TAG, "Unsccessful HHTP Response Code "+ responseCode);
}
Log.i(TAG, "Code " + responseCode);
}
catch (MalformedURLException e) {
Log.e(TAG, "exception caught: ", e);
}
catch (IOException e) {
Log.e(TAG, "exception IO caught: ", e);
}
catch(Exception e){
Log.e(TAG, "General exception", e);
}
NOW thats giving me the following in LOG CAT
02-21 03:13:08.350: D/dalvikvm(838): GC_FOR_ALLOC freed 43K, 5% free 2949K/3072K, paused 153ms, total 154ms
02-21 03:13:08.350: I/dalvikvm-heap(838): Grow heap (frag case) to 3.558MB for 635812-byte allocation
02-21 03:13:08.460: D/dalvikvm(838): GC_FOR_ALLOC freed 2K, 4% free 3567K/3696K, paused 101ms, total 101ms
02-21 03:13:09.310: I/Choreographer(838): Skipped 66 frames! The application may be doing too much work on its main thread.
02-21 03:13:09.460: D/gralloc_goldfish(838): Emulator without GPU emulation detected.
02-21 03:13:15.110: D/dalvikvm(838): GC_FOR_ALLOC freed 188K, 7% free 3892K/4156K, paused 171ms, total 208ms
02-21 03:13:15.350: D/dalvikvm(838): GC_FOR_ALLOC freed 375K, 13% free 3898K/4464K, paused 60ms, total 79ms
02-21 03:13:15.350: E/MainListActivity(838): General exception
02-21 03:13:15.350: E/MainListActivity(838): org.json.JSONException: Unterminated string at character 21961 of [{"Hospital":{"id":"61790","hospital_name":"Goodland Regional Medical Ctr","hospital_add_1":"220 W 2nd St","hospital_add_2":null,"hospital_city":"Goodland","hospital_state":"KS","hospital_zip":"67735","hospital_phone":"785-890-3625\r","hospital_fax":null,"hospital_description":null,"callcenter_agent_approval":"0","hospital_site":"","mdpocket_approval":"0","facebook":""},"Floor":[],"Department":[],"Image":[],"Notes":[]},{"Hospital":{"id":"61795","hospital_name":"Cheyenne County Hospital","hospital_add_1":"210 W 1st St","hospital_add_2":null,"hospital_city":"Saint Francis","hospital_state":"KS","hospital_zip":"67756","hospital_phone":"785-332-2104\r","hospital_fax":null,"hospital_description":null,"callcenter_agent_approval":"0","hospital_site":"","mdpocket_approval":"0","facebook":""},"Floor":[],"Department":[],"Image":[],"Notes":[]},{"Hospital":{"id":"61801","hospital_name":"Stanton County Hospital","hospital_add_1":"404 N Chestnut St","hospital_add_2":null,"hospital_city":"Johnson","hospital_state":"KS","hospital_zip":"67855","hospital_phone":"620-492-6250\r","hospital_fax":null,"hospital_description":null,"callcenter_agent_approval":"0","hospital_site":"","mdpocket_approval":"0","facebook":""},"Floor":[],"Department":[],"Image":[],"Notes":[]},{"Hospital":{"id":"61802","hospital_name":"Kearny County Hospital","hospital_add_1":"500 E Thorpe St","hospital_add_2":null,"hospital_city":"Lakin","hospital_state":"KS","hospital_zip":"67860","hospital_phone":"620-355-7111\r","hospital_fax":null,"hospital_description":null,"callcenter_agent_approval":"0","hospital_site":"","mdpocket_approval":"0","facebook":""},"Floor":[],"Department":[],"Image":[],"Notes":[]},{"Hospital":{"id":"61803","hospital_name":"Wichita County Health Center","hospital_add_1":"211 E Earl St","hospital_add_2":null,"hospital_city":"Leoti","hospital_state":"KS","hospital_zip":"67861","hospital_phone":"620-375-2233\r","hospital_fax":null,"hospital_description":null,"callcenter_agent_approval":"0","hospital_site":"","mdpocket_approval":"0","facebook":""},"Floor":[],"Department":[],"Image":[],"Notes":[]},{"Hospital":{"id":"61808","hospital_name":"Hamilton County Hospital","hospital_add_1":"700 N Huser St","hospital_add_2":null,"hospital_city":"Syracuse","hospital_state":"KS","hospital_zip":"67878","hospital_phone":"620-384-7461\r","hospital_fax":null,"hospital_description":null,"callcenter_agent_approval":"0","hospital_site":"","mdpocket_approval":"0","facebook":""},"Floor":[],"Department":[],"Image":[],"Notes":[]},{"Hospital":{"id":"61809","hospital_name":"Greeley County Health Services","hospital_add_1":"506 3rd St","hospital_add_2":null,"hospital_city":"Tribune","hospital_state":"KS","hospital_zip":"67879","hospital_phone":"620-376-4221\r","hospital_fax":null,"hospital_description":null,"callcenter_agent_approval":"0","hospital_site":"","mdpocket_approval":"0","facebook":""},"Floor":[],"Department":[],"Image":[],"Notes":[]},{"Hospital":{"id":"61810","hospital_name":"Bob Wilson Memorial Hospital","hospital_add_1":"415 N Main St","hospital_add_2":null,"hospital_city":"Ulysses","hospital_state":"KS","hospital_zip":"67880","hospital_phone":"620-356-1266\r","hospital_fax":null,"hospital_description":null,"callcenter_agent_approval":"0","hospital_site":"","mdpocket_approval":"0","facebook":""},"Floor":[],"Department":[],"Image":[],"Notes":[]},{"Hospital":{"id":"61812","hospital_name":"Morton County Hospital","hospital_add_1":"445 Hilltop St","hospital_add_2":null,"hospital_city":"Elkhart","hospital_state":"KS","hospital_zip":"67950","hospital_phone":"620-697-2141\r","hospital_fax":null,"hospital_description":null,"callcenter_agent_approval":"0","hospital_site":"","mdpocket_approval":"0","facebook":""},"Floor":[],"Department":[],"Image":[],"Notes":[]},{"Hospital":{"id":"61813","hospital_name":"Stevens County Hospital","hospital_add_1":"1006 S Jackson St","hospital_add_2":null,"hospital_city":"Hugoton","hospital_state":"KS","hospital_zip":"67951",
// CATLOG FINISHED
//this is not part of CATLOG JSON
[{"Hospital":{"id":"63083","hospital_name":"Colorado Mental Health Inst","hospital_add_1":"1600 W 24th St","hospital_add_2":null,"hospital_city":"Pueblo","hospital_state":"CO","hospital_zip":"81003","hospital_phone":"719-546-4000\r","hospital_fax":null,"hospital_description":null,"callcenter_agent_approval":"0","hospital_site":"","mdpocket_approval":"0","facebook":""},"Floor":[],"Department":[],"Image":[],"Notes":[]},{"Hospital":{"id":"63084","hospital_name":"Parkview Medical Center","hospital_add_1":"400 W 16th St","hospital_add_2":null,"hospital_city":"Pueblo","hospital_state":"CO","hospital_zip":"81003","hospital_phone":"719-584-4000\r","hospital_fax":null,"hospital_description":null,"callcenter_agent_approval":"0","hospital_site":"","mdpocket_approval":"0","facebook":""},"Floor":[],"Department":[],"Image":[],"Notes":[]},{"Hospital":{"id":"63085","hospital_name":"St Mary-Corwin Medical Center","hospital_add_1":"1008 Minnequa Ave","hospital_add_2":null,"hospital_city":"Pueblo","hospital_state":"CO","hospital_zip":"81004","hospital_phone":"719-560-4000\r","hospital_fax":null,"hospital_description":null,"callcenter_agent_approval":"0","hospital_site":"","mdpocket_approval":"0","facebook":""},"Floor":[],"Department":[],"Image":[],"Notes":[]}]