Danny Gloudemans
Danny Gloudemans

Reputation: 2677

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!

Upvotes: 3

Views: 2239

Answers (1)

Kongol
Kongol

Reputation: 403

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.

Upvotes: 5

Related Questions