Android使用JSON url在List中加载图像

时间:2012-01-01 12:38:36

标签: android json image tumblr

我目前有这个代码与purl.getString(“url”)包含在线jpg图像的url链接。我想在列表或网格中显示这些图像,但无法理解如何。当前代码只列出了listview中的所有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;
}

  }

0 个答案:

没有答案