使用tweet4j REST API不以Raw Json格式获取推文

时间:2014-03-14 04:47:24

标签: json twitter twitter4j

我使用twitter4j REST API使用其推文ID检索特定的推文。我的代码如下。我可以下载推文,但它不是原始的JSON格式。使用相同的推文ID从https://dev.twitter.com/console获取的原始JSON格式不同 twitter4j。请帮帮我,如何获取实际的JSON文件。谢谢!

使用tweet4j重新发送推文的代码

package org.hi.hello;
import twitter4j.*;
import twitter4j.auth.AccessToken;
import twitter4j.json.DataObjectFactory;
import java.io.IOException;

public class TweetUsingTwitter4jExample {

    public static void main(String[] args) throws IOException, TwitterException {

        //Your Twitter App's Consumer Key
        String consumerKey = "Zi03QNQ***9FVBRol0P2Kw";

        //Your Twitter App's Consumer Secret
        String consumerSecret = "sHKCBKeMD71jBUqA1J3dGDIsnQgw********";

        //Your Twitter Access Token
        String accessToken = "2383034234-reX0iWmtgUAaLdvCx1*******MvV4ItRwcJqytX";

        //Your Twitter Access Token Secret
        String accessTokenSecret = "aFn216CcmSmUTJjmrC9*****bz53hYUjIf7GqSeh90K";

        //Instantiate a re-usable and thread-safe factory
        TwitterFactory twitterFactory = new TwitterFactory();

        //Instantiate a new Twitter instance
        Twitter twitter = twitterFactory.getInstance();

        //setup OAuth Consumer Credentials
        twitter.setOAuthConsumer(consumerKey, consumerSecret);

        //setup OAuth Access Token
        twitter.setOAuthAccessToken(new AccessToken(accessToken, accessTokenSecret));


        Status s = twitter.showStatus(Long.parseLong("126417285559762944")); 
        System.out.println(s.toString());


        }

}

输出

StatusJSONImpl{createdAt=Wed Oct 19 03:30:04 IST 2011, id=126417285559762944, text='RT @Jewelz2611 @mashable @apple, iphones r 2 expensive. Most went w/ htc/galaxy. No customer loyalty w/phone comp..', source='<a href="http://geekking.in" rel="nofollow">_GeekKing</a>', isTruncated=false, inReplyToStatusId=-1, inReplyToUserId=-1, isFavorited=false, isRetweeted=false, favoriteCount=0, inReplyToScreenName='null', geoLocation=null, place=null, retweetCount=0, isPossiblySensitive=false, isoLanguageCode='null', lang='en', contributorsIDs=[], retweetedStatus=null, userMentionEntities=[UserMentionEntityJSONImpl{name='Jewelz', screenName='Jewelz2611', id=18176023}, UserMentionEntityJSONImpl{name='Mashable', screenName='mashable', id=972651}, UserMentionEntityJSONImpl{name='@apple', screenName='Apple', id=380749300}], urlEntities=[], hashtagEntities=[], mediaEntities=[], symbolEntities=[], currentUserRetweetId=-1, user=UserJSONImpl{id=20256514, name='PunjabiGeek', screenName='_GeekKing', location='India', description='Tech Geek, Gadgets fan, RC Fan,', isContributorsEnabled=false, profileImageUrl='http://pbs.twimg.com/profile_images/442731191255314432/8Li7PQCN_normal.png', profileImageUrlHttps='https://pbs.twimg.com/profile_images/442731191255314432/8Li7PQCN_normal.png', url='null', isProtected=false, followersCount=1069, status=null, profileBackgroundColor='000000', profileTextColor='000000', profileLinkColor='CC3300', profileSidebarFillColor='F7DA93', profileSidebarBorderColor='FFFFFF', profileUseBackgroundImage=true, showAllInlineMedia=false, friendsCount=933, createdAt=Fri Feb 06 23:35:47 IST 2009, favouritesCount=37, utcOffset=19800, timeZone='New Delhi', profileBackgroundImageUrl='http://pbs.twimg.com/profile_background_images/131659776/twitter.jpg', profileBackgroundImageUrlHttps='https://pbs.twimg.com/profile_background_images/131659776/twitter.jpg', profileBackgroundTiled=false, lang='en', statusesCount=26455, isGeoEnabled=false, isVerified=false, translator=false, listedCount=46, isFollowRequestSent=false}}

我使用以下网址https://dev.twitter.com/consolehttps://api.twitter.com/1.1/statuses/show/126417285559762944.json获得的内容:{{3}}

{
      "created_at": "Tue Oct 18 22:00:04 +0000 2011",
      "id": 126417285559762940,
      "id_str": "126417285559762944",
      "text": "RT @Jewelz2611 @mashable @apple, iphones r 2 expensive. Most went w/ htc/galaxy. No customer loyalty w/phone comp..",
      "source": "<a href="http://geekking.in" rel="nofollow">_GeekKing</a>",
      "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": 20256514,
        "id_str": "20256514",
        "name": "PunjabiGeek",
        "screen_name": "_GeekKing",
        "location": "India",
        "description": "Tech Geek, Gadgets fan, RC Fan,",
        "url": null,
        "entities": {
          "description": {
            "urls": []
          }
        },
        "protected": false,
        "followers_count": 1069,
        "friends_count": 933,
        "listed_count": 47,
        "created_at": "Fri Feb 06 18:05:47 +0000 2009",
        "favourites_count": 37,
        "utc_offset": 19800,
        "time_zone": "New Delhi",
        "geo_enabled": false,
        "verified": false,
        "statuses_count": 26453,
        "lang": "en",
        "contributors_enabled": false,
        "is_translator": false,
        "is_translation_enabled": false,
        "profile_background_color": "000000",
        "profile_background_image_url": "http://pbs.twimg.com/profile_background_images/131659776/twitter.jpg",
        "profile_background_image_url_https": "https://pbs.twimg.com/profile_background_images/131659776/twitter.jpg",
        "profile_background_tile": false,
        "profile_image_url": "http://pbs.twimg.com/profile_images/442731191255314432/8Li7PQCN_normal.png",
        "profile_image_url_https": "https://pbs.twimg.com/profile_images/442731191255314432/8Li7PQCN_normal.png",
        "profile_link_color": "CC3300",
        "profile_sidebar_border_color": "FFFFFF",
        "profile_sidebar_fill_color": "F7DA93",
        "profile_text_color": "000000",
        "profile_use_background_image": true,
        "default_profile": false,
        "default_profile_image": false,
        "following": false,
        "follow_request_sent": false,
        "notifications": false
      },
      "geo": null,
      "coordinates": null,
      "place": null,
      "contributors": null,
      "retweet_count": 0,
      "favorite_count": 0,
      "entities": {
        "hashtags": [],
        "symbols": [],
        "urls": [],
        "user_mentions": [
          {
            "screen_name": "Jewelz2611",
            "name": "Jewelz",
            "id": 18176023,
            "id_str": "18176023",
            "indices": [
              3,
              14
            ]
          },
          {
            "screen_name": "mashable",
            "name": "Mashable",
            "id": 972651,
            "id_str": "972651",
            "indices": [
              15,
              24
            ]
          },
          {
            "screen_name": "Apple",
            "name": "@apple",
            "id": 380749300,
            "id_str": "380749300",
            "indices": [
              25,
              31
            ]
          }
        ]
      },
      "favorited": false,
      "retweeted": false,
      "lang": "en"
    }

1 个答案:

答案 0 :(得分:3)

尝试将状态对象转换为自己为我工作的JSON 这是使用gson的实用方法

public static String copyToJsonString(Object modelObject ){
    String data=null;
    try {
        Gson gson= new Gson();
        data=gson.toJson(modelObject);
        return data;
    } catch (Exception e) {
        e.printStackTrace();
    } 
}