Android使用JSON url加载List中的图片
我目前有这段代码,其中 purl.getString("url") 包含在线 jpg 图像的 url 链接。我想在列表或网格中显示这些图像,但不明白如何操作。当前的代码只是列出了列表视图中的所有 URL,但只希望这些 URL 成为它们各自的图像。
这是我的代码
public class BreadActivity extends ListActivity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setListAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, this.fetchJsonStuff()));
}
public ArrayList<String> fetchJsonStuff()
{
ArrayList<String> listItems = new ArrayList<String>();
try {
URL example = new URL(
"http://api.tumblr.com/v2/blog/example.tumblr.com");
URLConnection tc = example.openConnection();
BufferedReader in = new BufferedReader(new InputStreamReader(
tc.getInputStream()));
String line;
while ((line = in.readLine()) != null) {
JSONObject ob = new JSONObject(line);
JSONObject object = ob.getJSONObject("response");
JSONArray ja = object.getJSONArray("posts");
for (int i = 0; i < ja.length(); i++) {
JSONObject jo = (JSONObject) ja.get(i);
JSONArray nja = (JSONArray) jo.getJSONArray("photos");
for (int i1 = 0; i1 < nja.length(); i1++) {
JSONObject njo = (JSONObject) nja.get(i1);
JSONObject purl = (JSONObject) njo.getJSONObject("original_size");
listItems.add(purl.getString("url"));
}
}
}
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return listItems;
}
}
I currently have this code with the purl.getString("url") containing url links to jpg images online. I would like to display these images in a list or grid but cannot understand how. The current code simply lists all of the URLs in listview but just want those URL's to be their respective images.
Here is my code
public class BreadActivity extends ListActivity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setListAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, this.fetchJsonStuff()));
}
public ArrayList<String> fetchJsonStuff()
{
ArrayList<String> listItems = new ArrayList<String>();
try {
URL example = new URL(
"http://api.tumblr.com/v2/blog/example.tumblr.com");
URLConnection tc = example.openConnection();
BufferedReader in = new BufferedReader(new InputStreamReader(
tc.getInputStream()));
String line;
while ((line = in.readLine()) != null) {
JSONObject ob = new JSONObject(line);
JSONObject object = ob.getJSONObject("response");
JSONArray ja = object.getJSONArray("posts");
for (int i = 0; i < ja.length(); i++) {
JSONObject jo = (JSONObject) ja.get(i);
JSONArray nja = (JSONArray) jo.getJSONArray("photos");
for (int i1 = 0; i1 < nja.length(); i1++) {
JSONObject njo = (JSONObject) nja.get(i1);
JSONObject purl = (JSONObject) njo.getJSONObject("original_size");
listItems.add(purl.getString("url"));
}
}
}
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return listItems;
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论