简体   繁体   中英

How to get more tweets by using twitter4j and parse to json?

I am using twitter4j to retrieve tweets from Twitter. As we know, we can only retrieve tweets less than 7 days. Also we can only get 100 real time tweets. But I want to retrieve more than that. After searching, I found that we can use setSince and setUntil to get more than 100 tweets . But when I used setSince and setUntil , I still can only get real-time tweets. Besides, I want to know how to parse my tweets to json format ?
Here's my code:

public static void main(String[] args) throws TwitterException, IOException, ClassNotFoundException, IllegalAccessException, InstantiationException, SQLException 
    {

        ConfigurationBuilder cb = new ConfigurationBuilder();
        cb.setDebugEnabled(true)
                .setOAuthConsumerKey("XXX")
                .setOAuthConsumerSecret("XXX")
                .setOAuthAccessToken("XXX")
                .setOAuthAccessTokenSecret("XX");
        TwitterFactory tf = new TwitterFactory(cb.build());
        Twitter twitter = tf.getInstance();
        Query query = new Query("KEYWORD");
        query.setCount(100);
        query.setSince("2013-06-27");
        query.setUntil("2013-07-02");
        query.geoCode(new GeoLocation(XXX, XXX), 200, Query.KILOMETERS);
        QueryResult result = twitter.search(query);
        System.out.println(result);
}

If you are trying to get the 1000 most recent tweets in a search, for example, you should use query.setMaxId() to the lowest # - 1 from your previous batch. If you are trying to get new(er) tweets since your last query, you should use query.setSinceId() to start from the value returned in result.getMaxId() from the previous batch.

See here

Instead of a query you could use getUserTimeline(userid, page)

int tweetCount = 250;
long userId = 12312323;  
twitter.getUserTimeline(userId, new Paging(1, tweetCount));

Documentation: http://twitter4j.org/oldjavadocs/3.0.0/twitter4j/api/TimelinesResources.html#getUserTimeline%28long,%20twitter4j.Paging%29

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM