I'm playing around with Twitter4J and i'm curious as to how i can return a single tweet (most recent tweet). My code is below... But it's not working. The documentation on the libraries website isn't great, but i've tried my best to follow it. But any input would be great.
It should also be noted that i'm trying to not use a loop of any description (will use loops in future projects)
Twitter twitter = TwitterFactory.getSingleton();
Query query = new Query("source:twitter4j LFC");
QueryResult result = twitter.search(query);
System.out.println("@" + status.getUser().getScreenName() + ":" + status.getText());
I do not see any relation between result
and status
variables, that's the reason it is not printing anything to you.
Try this,
Twitter twitter = TwitterFactory.getSingleton();
Query query = new Query("source:twitter4j LFC");
QueryResult result = twitter.search(query);
for (Status status : result.getTweets()) {
System.out.println("@" + status.getUser().getScreenName() + ":" + status.getText());
break; // You can delete the 'break' in future when you decide to implement/enhance the logic as you have stated
}
reference: http://twitter4j.org/en/code-examples.html (look at #4 Search for Tweets)