Search code examples
javaandroidtwittertwitter4j

Getting a user's username and their tweet from StatusJSONImpl that is returned from a query on a specific hashtag


I am running a query using Twitter4j to get any tweets that contain a specific hashtag. From this query I get a StatusJSONImpl which I am wondering how I can get each user's username and tweet. I don't need the date or id or anything like that and I am trying to avoid parsing through this manually.

So is there a way to get each individual user's username and tweet from this query I have made? Or is there a better way to do this?

Here is the code I use for the query:

//Textview to display hashtags
    hashtags = (TextView) findViewById(R.id.hashtags);
    hashtags.setTextColor(Color.WHITE);

    new Thread(new Runnable() 
    {

        @Override
        public void run() 
        {
            ConfigurationBuilder cb = new ConfigurationBuilder();
            cb.setDebugEnabled(true);
            cb.setOAuthConsumerKey(consumerkey);
            cb.setOAuthConsumerSecret(consumersecret);
            cb.setOAuthAccessToken(accesstoken);
            cb.setOAuthAccessTokenSecret(accesstokensecret);

            try
            {
                Twitter twitter = new TwitterFactory(cb.build()).getInstance();
                Query query = new Query(st1);
                QueryResult result;

                do
                {
                    result = twitter.search(query);
                    tweets = result.getTweets();
                } while ((query = result.nextQuery()) != null);

                runOnUiThread(new Runnable() 
                {  
                    @Override
                    public void run() 
                    {
                        setList();
                    }//end run
                });//end runnable

            }//end try
            catch (TwitterException te)
            {
                te.printStackTrace();
            }//end catch
        }//end run

    }).start();

The code puts the query in a List (which is what tweets is).


Solution

  • With result.getTweets() you get a List<Status>, with your List you can get every username and every tweet text with a bucle

    for(Status tweet: tweets){
        String username = tweet.getUser().getScreenName();
        String text = tweet.getText();
    }
    

    And that's it