Search code examples
javatwittertwitter4jhashtag

Find twitter status using a hashtag


I have been trying to get some twitter statuses in my twitter client using this:

Query query = new Query("#something");
    QueryResult result = twitter.search(query);

    for (Status status : result.getTweets()) {
        System.out.println("@" + status.getUser().getScreenName() + ": " + status.getText());
       // arrayList.add( "@" + status.getUser().getScreenName() + ": " + status.getText());
    }

This works fine, but i am also trying to add this to an arraylist, this arraylist will be initialized after this query and then shown in a list in the UI.

This also work fine, but if I add another tweet on this hashtag while the client is running the application wont add it to my view in the UI.

I've done some research and found that I could use a Steam instead, and this is what i found :

    ConfigurationBuilder cb = new ConfigurationBuilder();
                    cb.setDebugEnabled(true)
                            .setOAuthConsumerKey("*********************")
                            .setOAuthConsumerSecret("*********************")
                            .setOAuthAccessToken("*********************")
                            .setOAuthAccessTokenSecret("*********************");
                    TwitterStream twitterStream = new TwitterStreamFactory(cb.build()).getInstance();

                    StatusListener listener = new StatusListener() {

                        @Override
                        public void onException(Exception arg0) {
                            // TODO Auto-generated method stub


                      .........

                        @Override
                        public void onStatus(Status status) {
                            User user = status.getUser();

                            // gets Username
                            String username = status.getUser().getScreenName();
                            System.out.println(username);
                            String profileLocation = user.getLocation();
                            System.out.println(profileLocation);
                            long tweetId = status.getId();
                            System.out.println(tweetId);
                            String content = status.getText();
                            System.out.println(content +"\n");

                        }


                      .........
                    FilterQuery fq = new FilterQuery("#something");

                    String keywords[] = {"ireland"};

                    fq.track(keywords);

                    twitterStream.addListener(listener);
                    twitterStream.filter(fq);

                }

}

This is where the problem appears, so while I am using the second solution with a stream, it won't pick up the hashtags, but the keyword will work.. So how do I get the stream to work with a hashtag?


Solution

  • After a while of researching I've found a solution.

    Using the TwitterFactory instead of TwitterStreamFactory I was able to use specific hastags like shown bellow.

     public static void startTweets() {
        ConfigurationBuilder cb = new ConfigurationBuilder();
    
                             cb.setOAuthConsumerKey("*********************")
                            .setOAuthConsumerSecret("*********************")
                            .setOAuthAccessToken("*********************")
                            .setOAuthAccessTokenSecret("*********************");
        Twitter twitter = new TwitterFactory(cb.build()).getInstance();
    
    
    }
    
        public static void getFeed(String hash)  throws TwitterException  {
    
            Query query = new Query("#"+hash);
    
            QueryResult result = twitter.search(query);
    
            for (Status status : result.getTweets()) {
                System.out.println("@" + status.getUser().getScreenName() + ": " + status.getText());
                String username =status.getUser().getScreenName() ;
    
                String message = status.getText();
                twitter.showUser(username).getLocation();
                int Followers = twitter.showUser(username).getFollowersCount();
    
                main.AddtoList(username,message, Followers);
    
    }