Android : how to get ArrayLists from particular JSONObects in adroid?

on Thursday, July 31, 2014


I have made an android activity ,In that i am parsing some data from JSON webservice and display it into a ListView,Now my problem is in my webservice each jsonObject is having Array of strings(image urls),So i dont know how to get each arrayList seperately,I have tried but got all the Arrayvalues into one arrayList,Please help me how to do seperate array per json object in android,my code and webservice is as below: webservice



http://dev.booking.blancatours.com/api/v1/cities


response



[
{
id: "1",
name: "Bodrum",
program: null,
program_link: null,
images_array: [
"http://dev.booking.blancatours.com/uploads/53d1f3fde123b.jpg",
"http://dev.booking.blancatours.com/uploads/53d1f3fde20d4.jpg",
"http://dev.booking.blancatours.com/uploads/53d1f3fde21c6.jpg",
"http://dev.booking.blancatours.com/uploads/53d1f43b43e37.jpg"
],
hotels: [
{
id: "3",
city_id: "1",
name: "Bodrum Hotel",
images_array: [ ]
}
]
},
{
id: "2",
name: "Bursa",
program: null,
program_link: null,
images_array: [
"http://dev.booking.blancatours.com/uploads/53d1f4ff167a8.jpg",
"http://dev.booking.blancatours.com/uploads/53d1f4ff168e1.jpg",
"http://dev.booking.blancatours.com/uploads/53d1f4ff169cb.jpg"
],
hotels: [
{
id: "4",
city_id: "2",
name: "Bursa Hotel",
images_array: [ ]
}
]
},
{
id: "3",
name: "Istanbul",
program: null,
program_link: null,
images_array: [
"http://dev.booking.blancatours.com/uploads/53d1f5d1927ce.jpg"
],
hotels: [
{
id: "2",
city_id: "3",
name: "Istanbul Hotel",
images_array: [ ]
}
]
},
{
id: "4",
name: "Bolu",
program: null,
program_link: null,
images_array: [
"http://dev.booking.blancatours.com/uploads/53d1f8f500afc.jpg"
],
hotels: [ ]
},
{
id: "5",
name: "Trabzon",
program: null,
program_link: null,
images_array: [ ],
hotels: [ ]
},
{
id: "6",
name: "Yalova",
program: null,
program_link: null,
images_array: [ ],
hotels: [ ]
},
{
id: "7",
name: "Sapanca",
program: null,
program_link: null,
images_array: [ ],
hotels: [ ]
},
{
id: "8",
name: "Antalya",
program: null,
program_link: null,
images_array: [ ],
hotels: [ ]
}
]


code



ListView lv_dest;
DestinationAdapter adapter;
private ProgressDialog pDialog;
JSONObject destinations = null;
JSONArray imgArray = null;
Header hdr;
JSONArray hotelArray = null;
Menu menu;
ArrayList<HashMap<String, String>> destList;
ArrayList<String> destiList, hotelList, hotel_images;
String id;
String name;
String program;
String program_link;
String hotel_id;
String city_id;
String hotel_name;
JSONArray hotel_images_array;
Intent i;
private class GetDestinations extends AsyncTask<Void, Void, Void> {

@Override
protected void onPreExecute() {
super.onPreExecute();
// Showing progress dialog
pDialog = new ProgressDialog(DestinationsActivity.this);
pDialog.setMessage(getResources().getString(R.string.wait));
pDialog.setCancelable(false);
pDialog.show();

}

@Override
protected Void doInBackground(Void... arg0) {
// Creating service handler class instance
BackendAPIService sh = new BackendAPIService();
System.out
.println("::::::::::::::::::DESTINATIONS URL:::::::>>>>>>>>>"
+ Const.API_DESTINATIONS);
// Making a request to url and getting response
String jsonStr = sh.makeServiceCall(Const.API_DESTINATIONS,
BackendAPIService.GET);

Log.d("Response: ", "> " + jsonStr);

if (jsonStr != null) {
try {
JSONArray jsonArr = new JSONArray(jsonStr);

// Getting JSON Array node
// yes

// looping through All Contacts
for (int i = 0; i < jsonArr.length(); i++) {

JSONObject c = jsonArr.getJSONObject(i);
id = c.getString("id");
name = c.getString("name");
program = c.getString("program");
program_link = c.getString("program_link");
imgArray = c.getJSONArray("images_array");
HashMap<String, String> Destimap = new HashMap<String, String>();
Destimap.put("id", id);
Destimap.put("name", name);
Destimap.put("program", program);
Destimap.put("program_link", program_link);

if (imgArray != null && imgArray.length() != 0) {
for (int j = 0; j < imgArray.length(); j++) {
destiList.add(imgArray.getString(j));
System.out
.println("::::::::::::::::::IMAGE URL:::::::::::::::::"
+ imgArray.getString(j));
}
}
hotelArray = c.getJSONArray("hotels");
if (hotelArray != null && hotelArray.length() != 0) {
for (int k = 0; k < hotelArray.length(); k++) {

JSONObject d = hotelArray.getJSONObject(k);
hotel_id = d.getString("id");
city_id = d.getString("city_id");
hotel_name = d.getString("name");

Destimap.put("hotel_id", hotel_id);
Destimap.put("city_id", city_id);
Destimap.put("hotel_name", hotel_name);

hotel_images_array = d
.getJSONArray("images_array");

if (hotel_images_array != null
&& hotel_images_array.length() != 0) {
for (int l = 0; l < hotel_images_array
.length(); l++) {
hotel_images.add(hotel_images_array
.getString(l));
}
}

}
}
destList.add(Destimap);

}
} catch (JSONException e) {
e.printStackTrace();
}
} else {
Log.e("ServiceHandler", "Couldn't get any data from the url");
}

return null;
}

@Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
// Dismiss the progress dialog
if (pDialog.isShowing())
pDialog.dismiss();
/**
* Updating parsed JSON data into ListView
* */
adapter = new DestinationAdapter(DestinationsActivity.this,
destList, destiList);
lv_dest.setAdapter(adapter);
}

}

0 comments:

Post a Comment