Android使用JSON url加载List中的图片

发布于 2024-12-23 16:08:19 字数 1981 浏览 1 评论 0原文

我目前有这段代码,其中 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文