Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Twitter4j multiple queries - Twitter API 1.1

How can I add a multiple queries to my search method? I would use this in a servlet.

This is my code now:

private static void searchQuery(Twitter twitter) {

    try {
        Query query = new Query("test soccer");
        QueryResult result;
        do {

            result = twitter.search(query);
            List<Status> tweets = result.getTweets();

            System.out.println("tweets: " + tweets.size());
            for (Status tweet : tweets) {
                System.out.println("@" + tweet.getUser().getScreenName() + " - " + tweet.getText());
            }
        } while ((query = result.nextQuery()) != null);
        System.exit(0);
    } catch (TwitterException te) {
        te.printStackTrace();
        System.out.println("Failed to search tweets: " + te.getMessage());
        System.exit(-1);
    }
}

Thanks for you help!

like image 566
Danny Gloudemans Avatar asked Jun 07 '26 23:06

Danny Gloudemans


1 Answers

You can make queries just joining them into the Query object, just like this

Query query = new Query("(test soccer) OR (test basquet) OR (test tenis)");

But you must take care of the max length of the query because twitter can say you that the query is too complex to understand.

like image 130
Kongol Avatar answered Jun 09 '26 11:06

Kongol



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!