Please disable your adblock and script blockers to view this page

Search this blog

Wednesday 15 March 2017

Fetch Tweets of a User using Twitter4J API and show in af:table in ADF & Java

In the previous blogs we learned about posting tweets on twitter timeline using Twitter4J API and sending direct meaages to followers using same API

This post is next in the series and about fetching a user tweets and showing in ADF Table , For this requirement we need to use same Consumer Key+ Secret and Access Token+Secret (Refer previous posts for this)

And In same way after authentication we can fetch a user tweets by using it's twitter handle. So for this requirement I have added a button to fetch tweets and a POJO based table to show fetched tweets in page

See the code to fetch tweets using user's twitter handle and it's retweets and favourites count, We can get tweets by passing page number as parameter and it returns a list of tweets

    //ArrayList to poplate data in af:table
    List<TweetList> tweets = new ArrayList();
    public void setTweets(List<TweetList> tweets) {
        this.tweets = tweets;

    public List<TweetList> getTweets() {
        return tweets;

    /**Send direct message to your followers using their twitter handle
     * @param actionEvent
    public void retrieveTweetsFromTimeline(ActionEvent actionEvent) {
        if (twitterHandleBind.getValue() != null) {
            try {
                //Get Twitter Factory instance
                Twitter twitter = new TwitterFactory().getInstance();
                //Authorize app using consumer key and secret and Access token,Secret
                twitter.setOAuthConsumer(consumerKey, consumerSecret);
                AccessToken accessToken = new AccessToken(token, tokenSecret);

                //First param of Paging() is the page number, and in same way we can get tweets for all pages
                Paging paging = new Paging(1, 100);
                //Put all tweets in a list
                List<Status> statuses;

                statuses = twitter.getUserTimeline(twitterHandleBind.getValue().toString(), paging);
                //Iterate over list to get tweet text and other properties and add to list
                for (Status status : statuses) {
                    tweets.add(new TweetList(status.getText(), status.getRetweetCount(), status.getFavoriteCount()));

            } catch (TwitterException e) {
        } else {
            FacesMessage errMsg = new FacesMessage("Please Enter Twitter Handle to fetch tweets");
            FacesContext.getCurrentInstance().addMessage(null, errMsg);

Now to show these tweets in af:table we need a POJO class to capture all columns of af:table
For detailed post - Populate af:table programmatically from managead bean using POJO

public class TweetList {
    public TweetList(String tweetText, int rtCnt, int fvrtCnt) {
        this.tweetText = tweetText;
        this.rtCnt = rtCnt;
        this.fvrtCnt = fvrtCnt;

    private String tweetText;
    //Retweet Count
    private int rtCnt;
    //Favourite Count
    private int fvrtCnt;

    public void setRtCnt(int rtCnt) {
        this.rtCnt = rtCnt;

    public int getRtCnt() {
        return rtCnt;

    public void setFvrtCnt(int fvrtCnt) {
        this.fvrtCnt = fvrtCnt;

    public int getFvrtCnt() {
        return fvrtCnt;

    public void setTweetText(String tweetText) {
        this.tweetText = tweetText;

    public String getTweetText() {
        return tweetText;

All done now run and check application

Sample ADF Application- Download
Cheers :) Happy Learning

No comments :

Post a Comment