2014-03-28 17 views
0

我一直在試圖弄清楚如何從Android中的Twitter隨機用戶獲取推文。在Android中從twitter API獲取錯誤的json

我正在使用僅應用程序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是不完整的,我不知道爲什麼:/。有人能幫助我嗎?我會欣賞它。謝謝。

+0

這不是直接回答你的問題,但我認爲如果你使用[twitter4j](http://twitter4j.org/en/index.html),你的生活會容易得多。在Android上很棒! –

回答

0

沒關係,我找到了解決方案。 JSON並不完整,它只是在logcat中表現出這種方式(Idk爲什麼)。 解決的辦法是做一個JSONarray這樣的:

JSONarray array = new JSONarray(resultfromtwitter); 

然後遍歷它。