0

I'm working on the REST API of Twitter using Twitter4J libraries, particularly on the https://api.twitter.com/1.1/search/tweets.json endpoint. I am quite aware of Twitter's own Streaming API, but I don't want to use that (at least for now). I have a method that queries the /search/tweets endpoint by a do-while loop, but I want the method's return to be in streaming fashion, so that I can print the results in the console simultaneously, instead of loading everything all at once. Here's the method:

public List<Status> readTweets(String inputQuery) {

    List<Status> tweets = new ArrayList<Status>();
    int counter = 0;

    try {
        RateLimitStatus rateLimit = twitter.getRateLimitStatus().get("/search/tweets");
        int limit = rateLimit.getLimit();

        Query query = new Query(inputQuery);
        QueryResult result;

        do {
            result = twitter.search(query);
            tweets.addAll(result.getTweets());
            counter++;
        } while ((query = result.nextQuery()) != null && counter < (limit - 1));
    } catch (TwitterException e) {
        e.printStackTrace();
        System.out.println("Failed to search tweets: " + e.getMessage());
        tweets = null;
    }

    return tweets;
}

What can you suggest? P.S. I don't want to put the console printing functionality inside this method.

Thanks.

Razib
  • 10,965
  • 11
  • 53
  • 80
oikonomiyaki
  • 7,691
  • 15
  • 62
  • 101
  • Why not streaming API? Seems thats exactly what you want! In rest API you will get all the tweets at once (upto limit), there is nothing you can do about it. – Ramanan Apr 16 '15 at 05:40
  • Ah yes, it seems that's the answer. However, I'm tied to Java 7, which does not have Streams (it's in Java 8). Any alternative? – oikonomiyaki Apr 16 '15 at 06:04
  • Twitter4j will work with java 7, it doesn't use java 8's stream. So you can definitely use streaming API – Ramanan Apr 16 '15 at 06:16

0 Answers0