简体   繁体   中英

Twitter4j counting the number of tweets found

Here's my method for finding a hashtag:

void getNewTweets()
{
  //try the search
  try
  {
    Query query = new Query(searchString);
    //get the last 50 tweets
    query.count(2);
    QueryResult result = twitter.search(query);
    tweets = result.getTweets();

    System.out.println(tweets); 
  }  
  //if there is an error then catch it and print it out
  catch (TwitterException te)
  {
    System.out.println("Failed to search tweets: " + te.getMessage());
    System.exit(-1);
  }
}

I'm using the Twitter4j library. How would I count the number of tweets found?

You can also use the QueryResult.getCount() function.

Query query = new Query(searchString);
QueryResult result = twitter.search(query);

int count = results.getCount();
System.out.println("tweet count: " + count);

Store the tweets in an ArrayList and fetch the size of the ArrayList to display the number of tweets.

ArrayList tweets = (ArrayList) result.getTweets();
System.out.println(tweets.size());

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM