I'm trying to create a image gallery in my app. I've got the images in JSON and I'd like to create a String Array from the JSON Objects in an AsyncTask so I can pop the String Array into Universal Image Loader. I think I've got the AsyncTask correctly getting the strings, but I'm stumped as to how to put the strings in an Array ie. images[] and imageDescriptions[]. My JSON looks like this:
{
"gallery" :
[
{
"id":"0001",
"galleryurl":"http://www.mysite.com/apps/wcbc/images/building0001.jpg",
"gallerydescr":"image description 1"
},
{
"id":"0002",
"galleryurl":"http://www.mysite.com/apps/wcbc/images/building00011.jpg",
"gallerydescr":"image description 2"
}
]
}
and I want the resulting images[] to look like this:
public static final String[] IMAGES = new String[] {
"http://www.mysite.com/apps/wcbc/images/building0001.jpg",
"http://www.mysite.com/apps/wcbc/images/building00011.jpg"
};
And here's my AsyncTask class where I want to parse the JSON into the String[]:
private class DownloadJSON extends AsyncTask<Void, Void, Void> {
@Override
protected Void doInBackground(Void... params) {
galleryArrList = new ArrayList<HashMap<String, String>>();
JGrid4Adapter jParser = new JGrid4Adapter();
// getting JSON string from URL
JSONObject jsonOb = jParser.getJSONFromUrl(jsonUrl);
try {
JSArrGallery = jsonOb.getJSONArray(TAG_GALLERY);
// looping through All gallery images
for (int i = 0; i < JSArrGallery.length(); i++) {
JSONObject galleryJO = JSArrGallery.getJSONObject(i);
String idStr = galleryJO.getString(TAG_ID);
String urlStr = galleryJO.getString(TAG_GALLERYURL);
String descrStr = galleryJO.getString(TAG_GALLERYDESCR);
//-- How to create String Array at this point?
}// -- END for loop
} catch (JSONException e) {
e.printStackTrace();
}// --- END Try
return null;
}
@Override
protected void onPostExecute(Void args) {
//--- do stuff here
}
}
Any help would be cool.
3 Answers 3
By Using Array
String AidStr[]=new String();
String AurlStr[]=new String();
String AdescrStr[]=new String();
for (int i = 0; i < JSArrGallery.length(); i++) {
JSONObject galleryJO = JSArrGallery.getJSONObject(i);
String idStr = galleryJO.getString(TAG_ID);
String urlStr = galleryJO.getString(TAG_GALLERYURL);
String descrStr = galleryJO.getString(TAG_GALLERYDESCR);
//-- How to create String Array at this point?
AidStr[i]=idStr;
AurlStr[i]=urlStr;
AdescStr[i]=descrStr;
}// -- END for loop
or
By Using ArrayList
ArrayList<String> AidStr=new ArrayList<String>();
ArrayList<String> AurlStr=new ArrayList<String>();
ArrayList<String>AdescrStr =new ArrayList<String>();
for (int i = 0; i < JSArrGallery.length(); i++) {
JSONObject galleryJO = JSArrGallery.getJSONObject(i);
String idStr = galleryJO.getString(TAG_ID);
String urlStr = galleryJO.getString(TAG_GALLERYURL);
String descrStr = galleryJO.getString(TAG_GALLERYDESCR);
//-- How to create String Array at this point?
AidStr.add(idstr);
AurlStr.add(urlStr);
AdescStr.add(descrStr);
}// -- END for loop
Comments
You need to have a way to retrieve the String array from your AsyncTask. To do this, make these changes to your code:
private class DownloadJSON extends AsyncTask<Void, Void, Object[]> {//-------THIS LINE
private Callback callback;//---THIS LINES
public DownloadJSON(Callback callback) {//---THESE LINES
this.callback = callback;
}
@Override
protected Void doInBackground(Void... params) {
galleryArrList = new ArrayList<HashMap<String, String>>();
JGrid4Adapter jParser = new JGrid4Adapter();
// getting JSON string from URL
JSONObject jsonOb = jParser.getJSONFromUrl(jsonUrl);
try {
JSArrGallery = jsonOb.getJSONArray(TAG_GALLERY);
// looping through All gallery images
String[] imagesArray = new String[JSArrGallery.length()];//------THIS LINE
String[] descriptionsArray = new String[JSArrGallery.length()];//------THIS LINE TOO
for (int i = 0; i < JSArrGallery.length(); i++) {
JSONObject galleryJO = JSArrGallery.getJSONObject(i);
String idStr = galleryJO.getString(TAG_ID);
String urlStr = galleryJO.getString(TAG_GALLERYURL);
String descrStr = galleryJO.getString(TAG_GALLERYDESCR);
imagesArray[i] = urlStr;//-------THIS LINE
descriptionsArray[i] = descrStr;//----THIS LINE TOO
}// -- END for loop
return new Object[]{imagesArray, descriptionsArray};//----THIS LINE
} catch (JSONException e) {
e.printStackTrace();
}// --- END Try
return null;
}
@Override
protected void onPostExecute(Object[] args) {
//---THESE LINES
if (args == null)
callback.call(null, null);
else
callback.call(args[0], args[1]);
}
//-----THESE LINES
public interface Callback {
public void call(String[] imageURLs, String[] imageDescriptions);
}
}
Finally, to bring this all together, do:
new DownloadJSON(new DownloadJSON.Callback() {
public void call(String[] imageURLs, String[] imageDescriptions) {
//TODO handle these arrays now
}
}).execute();
Comments
Use JSArrGallery.length() to initialize IMAGES Array before adding element in Array as:
JSArrGallery = jsonOb.getJSONArray(TAG_GALLERY);
IMAGES=new String[JSArrGallery.length()]; //<< initialize array here
for (int i = 0; i < JSArrGallery.length(); i++) {
//..your code to get image url fom JSONObject...
// add url to Array
IMAGES[i]=urlStr;
}
instead of Array use ArrayList for Storing images url if JSONArray size is dynamic
class container { string id; string whatever; string someotherprop;}or at least array/list ofMap<String, ObjectOrString>