为Android应用程序获取tumblr图像

时间:2012-09-14 14:22:22

标签: android json image api tumblr

我正在尝试使用JSON创建一个显示来自tumblr帐户的图片的应用。我这样做是通过改变一些代码来实现的,这个代码对于一个几乎完全相同的Twitter应用程序。 不幸的是,我发现tumblr api mush更难使用,我能找到的所有例子都是为php制作的,因此无济于事。 此行的nullPointerException:

for (int i = 0; i < arr.length(); i++) {

使用jsonlint.com验证JSON,如下所示:

"meta": {
    "status": 200,
    "msg": "OK"
},
"response": {
    "blog": {
        "title": "Facts and Chicks",
        "posts": 789,
        "name": "factsandchicks",
        "url": "http://factsandchicks.com/",
        "updated": 1347565227,
        "description": "Random facts and hot chicks, the best way to learn.\nWebsite and Concept by: GustoNYC",
        "ask": false
    },
    "posts": [
        {
            "blog_name": "factsandchicks",
            "id": 31474135003,
            "post_url": "http://factsandchicks.com/post/31474135003/ups-was-founded-by-two-teenagers-with-one-bicycle",
            "slug": "ups-was-founded-by-two-teenagers-with-one-bicycle",
            "type": "photo",
            "date": "2012-09-13 19:37:59 GMT",
            "timestamp": 1347565079,
            "state": "published",
            "format": "html",
            "reblog_key": "iWEenkAh",
            "tags": [
                "facts",
                "factsandchicks",
                "chicks",
                "UPS",
                "teenagers",
                "friends",
                "business",
                "America",
                "WTF",
                "money",
                "inspiration",
                "history"
            ],
            "highlighted": [],
            "note_count": 241,
            "source_url": "http://factsandchicks.com",
            "source_title": "factsandchicks.com",
            "caption": "<p>UPS was founded by two teenagers with one bicycle and $100 Borrowed from a Friend.</p>\n<p><a href=\"http://www.ups.com/content/corp/about/history/1929.html\" target=\"_blank\">source</a></p>",
            "link_url": "http://factsandchicks.com",
            "photos": [
                {
                    "caption": "",
                    "alt_sizes": [
                        {
                            "width": 640,
                            "height": 960,
                            "url": "http://25.media.tumblr.com/tumblr_m8fz0bLjJU1r3tp7bo1_1280.jpg"
                        },
                        {
                            "width": 500,
                            "height": 750,
                            "url": "http://25.media.tumblr.com/tumblr_m8fz0bLjJU1r3tp7bo1_500.jpg"
                        },
                        {
                            "width": 400,
                            "height": 600,
                            "url": "http://24.media.tumblr.com/tumblr_m8fz0bLjJU1r3tp7bo1_400.jpg"
                        },
                        {
                            "width": 250,
                            "height": 375,
                            "url": "http://25.media.tumblr.com/tumblr_m8fz0bLjJU1r3tp7bo1_250.jpg"
                        },
                        {
                            "width": 100,
                            "height": 150,
                            "url": "http://24.media.tumblr.com/tumblr_m8fz0bLjJU1r3tp7bo1_100.jpg"
                        },
                        {
                            "width": 75,
                            "height": 75,
                            "url": "http://24.media.tumblr.com/tumblr_m8fz0bLjJU1r3tp7bo1_75sq.jpg"
                        }
                    ],
                    "original_size": {
                        "width": 640,
                        "height": 960,
                        "url": "http://25.media.tumblr.com/tumblr_m8fz0bLjJU1r3tp7bo1_1280.jpg"
                    }
                }
            ]
        },

我的代码如下:

public class Example extends Activity {

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    ArrayList<Tweet> tweets;
    try {
        tweets = getTweets();
        ListView listView = (ListView) findViewById(R.id.ListViewId);
        listView.setAdapter(new UserItemAdapter(this, R.layout.listitem,
                tweets));
    } catch (ClientProtocolException 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();
    }

}

public class UserItemAdapter extends ArrayAdapter<Tweet> {
    private ArrayList<Tweet> tweets;

    public UserItemAdapter(Context context, int imageViewResourceId,
            ArrayList<Tweet> tweets) {
        super(context, imageViewResourceId, tweets);
        this.tweets = tweets;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View v = convertView;
        if (v == null) {
            LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            v = vi.inflate(R.layout.listitem, null);
        }

        Tweet tweet = tweets.get(position);
        if (tweet != null) {

            ImageView image = (ImageView) v.findViewById(R.id.avatar);

            if (image != null) {
                image.setImageBitmap(getBitmap(tweet.image_url));
            }
        }
        return v;
    }
}

public Bitmap getBitmap(String bitmapUrl) {
    try {
        URL url = new URL(bitmapUrl);
        return BitmapFactory.decodeStream(url.openConnection()
                .getInputStream());
    } catch (Exception ex) {
        return null;
    }
}

public ArrayList<Tweet> getTweets() throws ClientProtocolException,
        IOException, JSONException {
    String searchUrl = "http://api.tumblr.com/v2/blog/factsandchicks.com/posts?api_key=API_KEY";

    ArrayList<Tweet> tweets = new ArrayList<Tweet>();

    HttpClient client = new DefaultHttpClient();
    HttpGet get = new HttpGet(searchUrl);

    ResponseHandler<String> responseHandler = new BasicResponseHandler();

    String responseBody = null;
    try {
        responseBody = client.execute(get, responseHandler);
    } catch (Exception ex) {
        ex.printStackTrace();
    }

    JSONArray posts = JSONObject.getJSONObject("response").getJSONArray(
            "posts");

    for (int i = 0; i < posts.length(); i++) {
        JSONArray photos = posts.getJSONObject(i).getJSONArray("photos");
        for (int j = 0; j < photos.length(); j++) {
            JSONObject photo = photos.getJSONObject(j);
            String url = photo.getJSONArray("alt_sizes").getJSONObject(0)
                    .getString("url");

            Tweet tweet = new Tweet(url);
            tweets.add(tweet);
    }

    return tweets;
}

public class Tweet {

    public String image_url;

    public Tweet(String url) {

        this.image_url = url;
    }
}

}

我不知道为什么这不起作用,因为我可以让它适用于Twitter。 任何帮助都是适当的!

1 个答案:

答案 0 :(得分:1)

您从JSON读取“响应”作为数组,但它不是数组,只是一个对象。似乎这是一个问题。你不是在读取对象,比如他们在JSON中的表现,更加注意!

尝试做这样的事情:

JSONArray posts = jsonObject.getJSONObject("response").getJSONArray("posts");
for(int i = 0; i < posts.length(); i++){
    JSONArray photos = posts.getJSONObject(i).getJSONArray("photos");
    for(int j = 0; j < photos.length(); j++){
        JSONObject photo = photos.getJSONObject(j);
        String url = photo.getJSONArray("alt_sizes").getJSONObject(0).getString("url");

        Tweet tweet = new Tweet(url);
        tweets.add(tweet);
    }
}

此刻我无法测试,希望我没有犯任何错误。