Retriving the latest tweets in Twitter4J

edited November 2013 in Library Questions

How do I retrieve the latest tweets for a hash tag? Im using twitter4j 3 and processing 2

So far I can only retrieve only a given number of tweets, but I would like to retrieve the latest tweets for the hash tag #willsmith for example:

Heres my code:

void connectTwitter() {  
  ConfigurationBuilder cb = new ConfigurationBuilder();  
  cb.setOAuthConsumerKey("XXXXXXX");
  cb.setOAuthConsumerSecret("XXXXXXX");
  cb.setOAuthAccessToken("XXXXXXX");
  cb.setOAuthAccessTokenSecret("XXXXXX"); 

  twitterFactory = new TwitterFactory(cb.build());    
  twitter = twitterFactory.getInstance();  

  println("connected");
} 

// Get your tweets
void getTimeline() {     
  try {        
    statuses = twitter.getHomeTimeline();
  }   
  catch(TwitterException e) {         
    println("Get timeline: " + e + " Status code: " + e.getStatusCode());
  }     
  for (Status status:statuses) {               
    println(status.getUser().getName() + ": " + status.getText());
  }
}  
// Search for tweets

void getSearchTweets() {           
  try {        
    Query query = new Query("#willsmith");   
    query.setCount(100);    
    QueryResult result = twitter.search(query);  
    for (Status status : result.getTweets()) {              
      println("@" + status.getUser().getScreenName() + ":" + status.getText());
    }
  }   
  catch (TwitterException e) {            
    println("Search tweets: " + e);
  }
}

Any suggestions?

Answers

Sign In or Register to comment.