Saturday Aug 31

Retrieve Tweets using Twitter4J

PDF Print E-mail
Friday, 15 July 2011 12:47
AddThis Social Bookmark Button

Retrieve Twitter Tweets using Twitter4J

The following article describes how to retrieve Tweets using Twitter4J library.Let's imaginge we want to retrieve Tweets from the @TheJavaTutorial Tweeter user.  Let's take a look

Twitter java source using Twitter4J

package com.ubiteck.sample;

import java.util.logging.Logger;

import twitter4j.Query;
import twitter4j.QueryResult;
import twitter4j.Status;
import twitter4j.Tweet;
import twitter4j.Twitter;
import twitter4j.TwitterException;
import twitter4j.TwitterFactory;
import twitter4j.auth.AccessToken;
import twitter4j.auth.RequestToken;

 * Twitter application using Twitter4J
public class TwitterApplication {
    private final Logger logger = Logger.getLogger(TwitterApplication.class.getName());

    public static void main(String[] args) {
        new TwitterApplication().retrieve();

    public void retrieve() {"Retrieving tweets...");
        Twitter twitter = new TwitterFactory().getInstance();
        String user = "TheJavaTutorial";
        Query query = new Query("from:"+user);
        try {
            QueryResult result =;
            System.out.println("Count : " + result.getTweets().size()) ;
            for (Tweet tweet : result.getTweets()) {
                System.out.println("text : " + tweet.getText());
        } catch (TwitterException e) {
        }"done! ");

Twitter Factory

Twitter twitter = new TwitterFactory().getInstance();

Twitter factory return a thread safe instance of the twitter service

Twitter Query

        Query query = new Query("from:"+user);

Query is a data class representing a search query. It is not thread safe.
The from keyword indicates we want to find tweets from the provided user.

Twitter provided several other search operators .

Twitter Query


Query.setRpp method indicates the number of tweets returned per page, up to the max 100.

Query.setSince returns tweets with since the given date

That's all. If you found this article usefull click on the google +1 button below to recommend our website. Thanks in advance

Twitter search operators

Operator Finds tweets...
twitter search containing both "twitter" and "search". This is the default operator.
"happy hour" containing the exact phrase "happy hour".
love OR hate containing either "love" or "hate" (or both).
beer -root containing "beer" but not "root".
#haiku containing the hashtag "haiku".
from:alexiskold sent from person "alexiskold".
to:techcrunch sent to person "techcrunch".
@mashable referencing person "mashable".
"happy hour" near:"san francisco" containing the exact phrase "happy hour" and sent near "san francisco".
near:NYC within:15mi sent within 15 miles of "NYC".
superhero since:2011-07-14 containing "superhero" and sent since date "2011-07-14" (year-month-day).
ftw until:2011-07-14 containing "ftw" and sent up to date "2011-07-14".
movie -scary :) containing "movie", but not "scary", and with a positive attitude.
flight :( containing "flight" and with a negative attitude.
traffic ? containing "traffic" and asking a question.
hilarious filter:links containing "hilarious" and linking to URLs.
news source:twitterfeed containing "news" and entered via TwitterFeed

If you have any remark or questions feel free to put a comment.If you enjoyed this tutorial and want to promote it don't hesitate to click on

Related Article

Tags: search , twitter , tweets , person , hour , 2011-07-14 , happy , query

Add comment

Security code

Java Tutorial on Facebook