I am tryi开发者_运维知识库ng to search a tweets using search method in twitter4j with effects similar to this website:Monitter.com
Like this website, I want to display a tweets in real time twitter updates. I just want the tweets to be loaded in a similar manner. So how do i search tweets in similar to this website(Monitter.com)?
Please could someone show me a simple query examples.
I'm assuming that you already know how to use the twitter4j examples. If you look at the SearchTweets.java
file under "twitter4j-2.2.5\twitter4j-examples\src\main\java\twitter4j\examples\search", the following code will be relevant to you:
try {
QueryResult result = twitter.search(new Query(args[0]));
List<Tweet> tweets = result.getTweets();
for (Tweet tweet : tweets) {
System.out.println("@" + tweet.getFromUser() + " - " + tweet.getText());
}
System.exit(0);
} catch (TwitterException te) {
te.printStackTrace();
System.out.println("Failed to search tweets: " + te.getMessage());
System.exit(-1);
}
For real-time updating like Monitter, you should encase this chunk in a loop that runs at regular intervals (check out Thread.sleep()
). Next, you may want to shorten the number of results retrieved based on your frequency. For that, open up Query.java
under "twitter4j-2.2.5\twitter4j-examples\src\main\java\twitter4j". The default Query for SearchTweets.java
uses only a String argument, but you'll want to look at the setRpp()
and setSince()
methods.
I'm still new to this too so I can't be certain about it, but I hope my answer helps! I'd have commented instead but I don't have sufficient privileges.
精彩评论