2

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);
}

2 Answers 2

0

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

0

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.