我正在尝试在我的应用中创建图片库。我有JSON中的图像,我想从AsyncTask
中的JSON对象创建一个字符串数组,这样我就可以将字符串数组弹出到Universal Image Loader中。我想我已经AsyncTask
正确地获取了字符串,但是我很难过如何将字符串放入数组中。 images[]
和imageDescriptions[]
。我的JSON看起来像这样:
{
"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"
}
]
}
我希望生成的images[]
看起来像这样:
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"
};
这是我的AsyncTask
课,我想将JSON解析为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
}
}
任何帮助都会很酷。
答案 0 :(得分:1)
使用数组
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
或
使用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
答案 1 :(得分:0)
您需要有一种方法从AsyncTask
中检索String数组。为此,请对代码进行以下更改:
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);
}
}
最后,为了实现这一切,请执行:
new DownloadJSON(new DownloadJSON.Callback() {
public void call(String[] imageURLs, String[] imageDescriptions) {
//TODO handle these arrays now
}
}).execute();
答案 2 :(得分:0)
在将数组中的元素添加为:
之前,使用JSArrGallery.length()
初始化IMAGES
数组
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;
}
如果ArrayList
尺寸是动态的,而不是数组使用JSONArray
来存储图片网址