【问题标题】:Getting wrong json from twitter API in Android从 Android 中的 twitter API 获取错误的 json
【发布时间】:2014-05-08 05:16:58
【问题描述】:

我一直试图弄清楚如何从 Android 中的 twitter 随机用户那里获取推文。

我正在使用仅限应用程序的 oauth 系统。我在获取不记名令牌或身份验证方面没有任何问题,当我询问用户时间线时,我的问题就出现了。

这是我的代码:

//Getting the data from the twitter api
                    HttpClient httpclient = new DefaultHttpClient();
                    //Append twitter count and screen name with api URL 
                    HttpGet httpget = new HttpGet("https://api.twitter.com/1.1/statuses/user_timeline.json?screen_name=twitterapi&count=2"); 
                    //Append bearer token here.
                    httpget.setHeader("Authorization", "Bearer "+ token);

                      response = httpclient.execute(httpget);

                      if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK)
                      {

                          HttpEntity entity = response.getEntity();
                         // String result = EntityUtils.toString(entity);
                          //Log.v("result", result);


                          InputStream content = entity.getContent();

                          StringBuilder builder = new StringBuilder();
                            BufferedReader reader = new BufferedReader(new InputStreamReader(content));
                            String line;
                            while ((line = reader.readLine()) != null) {
                              builder.append(line);
                            }

我从该代码得到的结果是:

[
{
    "created_at": "Thu Mar 27 20:10:19 +0000 2014",
    "id": 449277253860352000,
    "id_str": "449277253860352000",
    "text": "RT @TwitterDev: Streaming API will be \"replaying\" some Tweets starting at 1PM (Pacific) today. https:\/\/t.co\/YkpeBC8ixZ",
    "source": "web",
    "truncated": false,
    "in_reply_to_status_id": null,
    "in_reply_to_status_id_str": null,
    "in_reply_to_user_id": null,
    "in_reply_to_user_id_str": null,
    "in_reply_to_screen_name": null,
    "user": {
        "id": 6253282,
        "id_str": "6253282",
        "name": "Twitter API",
        "screen_name": "twitterapi",
        "location": "San Francisco, CA",
        "description": "The Real Twitter API. I tweet about API changes, service issues and happily answer questions about Twitter and our API. Don't get an answer? It's on my website.",
        "url": "http:\/\/t.co\/78pYTvWfJd",
        "entities": {
            "url": {
                "urls": [
                    {
                        "url": "http:\/\/t.co\/78pYTvWfJd",
                        "expanded_url": "http:\/\/dev.twitter.com",
                        "display_url": "dev.twitter.com",
                        "indices": [
                            0,
                            22
                        ]
                    }
                ]
            },
            "description": {
                "urls": [

                ]
            }
        },
        "protected": false,
        "followers_count": 2106712,
        "friends_count": 46,
        "listed_count": 12572,
        "created_at": "Wed May 23 06:01:13 +0000 2007",
        "favourites_count": 27,
        "utc_offset": -25200,
        "time_zone": "Pacific Time (US & Canada)",
        "geo_enabled": true,
        "verified": true,
        "statuses_count": 3485,
        "lang": "en",
        "contributors_enabled": false,
        "is_translator": false,
        "is_translation_enabled": false,
        "profile_background_color": "C0DEED",
        "profile_background_image_url": "http:\/\/pbs.twimg.com\/profile_background_images\/656927849\/miyt9dpjz77sc0w3d4vj.png",
        "profile_background_image_url_https": "https:\/\/pbs.twimg.com\/profile_background_images\/656927849\/miyt9dpjz77sc0w3d4vj.png",
        "profile_background_tile": true,
        "profile_image_url": "http:\/\/pbs.twimg.com\/profile_images\/2284174872\/7df3h38zabcvjylnyfe3_normal.png",
        "profile_image_url_https": "https:\/\/pbs.twimg.com\/profile_images\/2284174872\/7df3h38zabcvjylnyfe3_normal.png",
        "profile_banner_url": "https:\/\/pbs.twimg.com\/profile_banners\/6253282\/1347394302",
        "profile_link_color": "0084B4",
        "profile_sidebar_border_color": "C0DEED",
        "profile_sidebar_fill_color": "DDEEF6",
        "profile_text_color": "333333",
        "profile_use_background_image": true,
        "default_profile": false,
        "default_profile_image": false,
        "following": null,
        "follow_request_sent": null,
        "notifications": null
    },
    "geo": null,
    "coordinates": null,
    "place": null,
    "contributors": null,
    "retweeted_status": {
        "created_at": "Thu Mar 27 19:32:51 +0000 2014",
        "id": 449267825870262272,
        "id_str": "449267825870262272",
        "text": "Streaming API will be \"replaying\" some Tweets starting at 1PM (Pacific) today. https:\/\/t.co\/YkpeBC8ixZ",
        "source": "\u003ca href=\"https:\/\/about.twitter.com\/products\/tweetdeck\" rel=\"nofollow\"\u003eTweetDeck\u003c\/a\u003e",
        "truncated": false,
        "in_reply_to_status_id": null,
        "in_reply_to_status_id_str": null,
        "in_reply_to_user_id": null,
        "in_reply_to_user_id_str": null,
        "in_reply_to_screen_name": null,
        "user": {
            "id": 2244994945,
            "id_str": "2244994945",
            "name": "TwitterDev",
            "screen_name": "TwitterDev",
            "location": "Internet",
            "description": "Developers and Platform Relations @Twitter. We are developers advocates. We can't answer all your questions, but we listen to all of them!",
            "url": "https:\/\/t.co\/66w26cua1O",
            "entities": {
                "url": {
                    "urls": [
                        {
                            "url": "https:\/\/t.co\/66w26cua1O",
                            "expanded_url": "https:\/\/dev.twitter.com\/",
                            "display_url": "dev.twitter.com",
                            "indices": [
                                0,
                                23
                            ]
                        }
                    ]
                },
                "description": {
                    "urls": [

                    ]
                }
            },
            "protected": false,
            "followers_count": 2942,
            "friends_count": 908,
            "listed_count": 50,
            "created_at": "Sat Dec 14 04:35:55 +0000 2013",
            "favourites_count": 60,
            "utc_offset": -25200,
            "time_zone": "Pacific Time (US & Canada)",
            "geo_enabled": false,
            "verified": true,
            "statuses_count": 199,
            "lang": "en",
            "contributors_enabled": false,
            "is_translator": false,
            "is_translation_enabled": false,
            "profile_background_color": "FFFFFF",
            "profile_background_image_url": "http:\/\/abs.twimg.com\/images\/themes\/theme1\/bg.png",
            "profile_background_image_url_https": "https:\/\/abs.twimg.com\/images\/themes\/theme1\/bg.png",
            "profile_background_tile": false,
            "profile_image_url": "http:\/\/pbs.twimg.com\/profile_images\/431949550836662272\/A6Ck-0Gx_normal.png",
            "profile_image_url_https": "https:\/\/pbs.twimg.com\/profil

这就是我所得到的,如您所见,Json 不完整,我不知道为什么 :/ 。有人能帮助我吗?我会很感激的。谢谢。

【问题讨论】:

  • 这不是您问题的直接答案,但我认为如果您使用twitter4j,您的生活会轻松很多。在 Android 上运行良好!

标签: android json twitter


【解决方案1】:

没关系,我找到了解决方案。 JSON 不是不完整的,它只是在 logcat 中以这种方式显示(Idk 为什么)。 解决方案是制作这样的 JSONarray:

JSONarray array = new JSONarray(resultfromtwitter);

然后对其进行迭代。

【讨论】:

    猜你喜欢
    • 2017-01-22
    • 2018-02-08
    • 1970-01-01
    • 1970-01-01
    • 2012-11-24
    • 2013-09-18
    • 2012-12-25
    • 2020-10-25
    • 2013-08-05
    相关资源
    最近更新 更多