我正在做我的BlogApp。它从Internet获取所有数据。我添加了三个TextView,但是从JSON获取图像时遇到了问题。我尝试了不同的方法,但是我仍然不知道该怎么做。这是我的代码的一部分:
private void handleBlogResponse() {
mProgressBar.setVisibility(View.INVISIBLE);
if (mBlogData==null){
updateDisplayForError();
}else {
try {
JSONArray jsonPosts = mBlogData.getJSONArray("posts");
ArrayList<HashMap<String, String>> blogPosts = new ArrayList<HashMap<String, String>>();
for (int i=0; i<jsonPosts.length(); i++){
JSONObject posts = jsonPosts.getJSONObject(i);
String title = posts.getString(KEY_TITLE); //"title"
title = Html.fromHtml(title).toString();
String author = posts.getString(KEY_AUTHOR); //"author"
author = Html.fromHtml(author).toString();
String time = posts.getString((KEY_TIME)); //"time"
time = Html.fromHtml(time).toString();
String icon = posts.getString(KEY_ICON); //"icon" (icons url)
icon = Html.fromHtml(icon).toString();
我在这里停了下来,不知道下一步该怎么做!可能应该为我的图像添加适配器以进行转换?
HashMap<String, String> blogPost = new HashMap<String, String>();
blogPost.put(KEY_TITLE, title);
blogPost.put(KEY_AUTHOR, author);
blogPost.put(KEY_TIME, time);
blogPosts.add(blogPost);
}
String[] keys = {KEY_TITLE, KEY_AUTHOR, KEY_TIME};
int[] ids = {R.id.textView1, R.id.textView2, R.id.textView3};
SimpleAdapter adapter = new SimpleAdapter(this, blogPosts, R.layout.row_layout,
keys, ids);
setListAdapter(adapter);
} catch (JSONException e) {
Log.e(TAG, "Exception caught" + e);
}
}
}
感谢您的提醒!
Volley Library使这项工作变得非常容易,并且可以自行处理所有其他相关任务。您可以使用ImageLoader或NetworkImageView。
请点击链接了解如何实现:https://developer.android.com/training/volley/request.html
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句