我正在使用imageloader从json响应中加载缩略图,有很多从json响应中获取的图像需要解析并将图像放在listview中。我正在混淆添加url[position]
在imgLoader.DisplayImage(url[position], loader, image);
中获取错误的任何人帮助我找到解决方案,这将对我有很大的帮助,我们将非常感谢您的回答。提前谢谢你。
以下是代码和json响应:
public class AndroidLoadImageFromURLActivity extends ListActivity {
// url to make request
private static String desi = "http://website";
// JSON Node names
private static final String TAG_POSTS = "posts";
private static final String TAG_ID = "id";
private static final String TAG_TITLE = "title";
private static final String TAG_DATE = "date";
private static final String TAG_CONTENT = "content";
private static final String TAG_AUTHOR = "author";
private static final String TAG_NAME = "name";
private static final String TAG_ATTACHMENTS = "attachments";
private static final String TAG_IMAGES = "images";
private static final String TAG_THUMBNAIL = "thumbnail";
private static final String TAG_URL = "url";
// contacts JSONArray
JSONArray posts = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.list_item);
// Hashmap for ListView
ArrayList<HashMap<String, String>> contactList = new ArrayList<HashMap<String, String>>();
// Creating JSON Parser instance
JSONParser jParser = new JSONParser();
// getting JSON string from URL
JSONObject json = jParser.getJSONFromUrl(desi);
try {
// Getting Array of Contacts
posts = json.getJSONArray(TAG_POSTS);
// looping through All Contacts
for(int i = 0; i < posts.length(); i++){
JSONObject c = posts.getJSONObject(i);
// Storing each json item in variable
String id = c.getString(TAG_ID);
String title = c.getString(TAG_TITLE);
String date = c.getString(TAG_DATE);
String content = c.getString(TAG_CONTENT);
// to remove all <P> </p> and <br /> and replace with ""
content = content.replace("<br />", "");
content = content.replace("<p>", "");
content = content.replace("</p>", "");
// Author is agin JSON Object
JSONObject author = c.getJSONObject(TAG_AUTHOR);
String name = author.getString(TAG_NAME);
JSONArray attachments = json.getJSONArray(TAG_ATTACHMENTS);
for(int j = 0; j < attachments.length(); j++){
JSONObject d = attachments.getJSONObject(i);
JSONObject thumbnail = d.getJSONObject(TAG_THUMBNAIL);
String url = thumbnail.getString(TAG_URL);
}
// creating new HashMap
HashMap<String, String> map = new HashMap<String, String>();
// adding each child node to HashMap key => value
map.put(TAG_ID, id);
map.put(TAG_TITLE, title);
map.put( TAG_DATE, date);
map.put( TAG_NAME, name);
map.put( TAG_CONTENT, content);
// adding HashList to ArrayList
contactList.add(map);
}
} catch (JSONException e) {
e.printStackTrace();
}
/**
* Updating parsed JSON data into ListView
* */
ListAdapter adapter = new SimpleAdapter(this, contactList,
R.layout.list_row,
new String[] { TAG_TITLE, TAG_DATE, TAG_NAME, TAG_CONTENT, TAG_URL },
new int[] { R.id.name, R.id.email,R.id.mobile, R.id.content, R.id.list_image});
setListAdapter(adapter);
// Loader image - will be shown before loading image
int loader = R.drawable.loader;
// Imageview to show
ImageView image = (ImageView) findViewById(R.id.list_image);
// ImageLoader class instance
ImageLoader imgLoader = new ImageLoader(getApplicationContext());
// whenever you want to load an image from url
// call DisplayImage function
// url - image url to load
// loader - loader image, will be displayed before getting image
// image - ImageView
imgLoader.DisplayImage(url[position], loader, image);
}
}
JSON回复
{
"status": "ok",
"posts": [
{
"id": 2498,
"title": "jigsaw lamp imported from thailand",
"content": "<p>Hi. It’s a invitation to have a look at a unique lamp shade called jigsaw lamp from thailand. Available in multi attractive colours.</p>\n",
"date": "2012-12-26 09:48:15",
"author": {
"name": "Tapas123456",
},
"attachments": [
{
"description": "",
"caption": "",
"mime_type": "image/jpeg",
"images": {
"thumbnail": {
"url": "http://site/wp-content/uploads/2012/12/646675-50x47.jpg",
}
}
},...............
logcat错误
12-26 16:53:26.154: E/AndroidRuntime(330): FATAL EXCEPTION: main
12-26 16:53:26.154: E/AndroidRuntime(330): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.androidhive.imagefromurl/com.androidhive.imagefromurl.AndroidLoadImageFromURLActivity}: java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list'
12-26 16:53:26.154: E/AndroidRuntime(330): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
12-26 16:53:26.154: E/AndroidRuntime(330): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
12-26 16:53:26.154: E/AndroidRuntime(330): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
12-26 16:53:26.154: E/AndroidRuntime(330): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
12-26 16:53:26.154: E/AndroidRuntime(330): at android.os.Handler.dispatchMessage(Handler.java:99)
12-26 16:53:26.154: E/AndroidRuntime(330): at android.os.Looper.loop(Looper.java:123)
12-26 16:53:26.154: E/AndroidRuntime(330): at android.app.ActivityThread.main(ActivityThread.java:4627)
12-26 16:53:26.154: E/AndroidRuntime(330): at java.lang.reflect.Method.invokeNative(Native Method)
12-26 16:53:26.154: E/AndroidRuntime(330): at java.lang.reflect.Method.invoke(Method.java:521)
12-26 16:53:26.154: E/AndroidRuntime(330): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
12-26 16:53:26.154: E/AndroidRuntime(330): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
12-26 16:53:26.154: E/AndroidRuntime(330): at dalvik.system.NativeStart.main(Native Method)
12-26 16:53:26.154: E/AndroidRuntime(330): Caused by: java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list'
12-26 16:53:26.154: E/AndroidRuntime(330): at android.app.ListActivity.onContentChanged(ListActivity.java:245)
12-26 16:53:26.154: E/AndroidRuntime(330): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:201)
12-26 16:53:26.154: E/AndroidRuntime(330): at android.app.Activity.setContentView(Activity.java:1647)
12-26 16:53:26.154: E/AndroidRuntime(330): at com.androidhive.imagefromurl.AndroidLoadImageFromURLActivity.onCreate(AndroidLoadImageFromURLActivity.java:44)
12-26 16:53:26.154: E/AndroidRuntime(330): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
12-26 16:53:26.154: E/AndroidRuntime(330): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
答案 0 :(得分:1)
您已将 url 变量声明为字符串 for for循环
以下是您的代码..
for(int j = 0; j < attachments.length(); j++)
{
JSONObject d = attachments.getJSONObject(i);
JSONObject thumbnail = d.getJSONObject(TAG_THUMBNAIL);
String url = thumbnail.getString(TAG_URL);
}
只需要将网址声明为url
为全局
如下面的代码。
String url;
for(int j = 0; j < attachments.length(); j++)
{
JSONObject d = attachments.getJSONObject(i);
JSONObject thumbnail = d.getJSONObject(TAG_THUMBNAIL);
url = thumbnail.getString(TAG_URL);
}
并使用url
imgLoader.DisplayImage(url, loader, image);