Twitter tutorial covering hashtags, reply & favorite/un-favorite using twitter4j

This article is in continuation to our previous article which demonstrated user how to authenticate on twitter and post tweet with/without image using twitter4j. This article will help you understand on how to fetch tweets using hashtags and other twitter actions like tweet, re-tweet etc . So lets begin tweeting…I mean the tutorial 🙂 #letsBegin In social media everyone is aware of #hashtags, here we will explain how to fetch tweets for a particular hash tags. we are using “Query” class to fetch tweets.


Query query = new Query(“#android”);

Here, above code snippet is to fetch tweets containing “#android” hashtag. Multiple hashtags You can also fetch tweets for more than one hashtags, say you want to fetch tweets containing hashtag #code or #android,  you need to include both tags with keyword “OR”. for eg:  “#code OR #android” ,  “#code AND #android” Now lets see the above code in action.


Query query = new Query(hashtag);

query.count(10); //  Query will return only 10 tweets per request.

QueryResult queryResultObject = twitter.search(query);

List<twitter4j.Status> qrTweets = queryResultObject.getTweets();

queryResultObject.getTweets()  will return list of twitter4j.Status, which contains tweet data  like retweet count as well as tweet’s owner info. for eg: user screen name, profile image etc Here is code snippet to fetch tweet related data from status model.


status.getUser().getName(); // to get user name

status.getId();// to get tweet id

status.getUser().getScreenName(); // to get user screen name/ user handler

status.getText();// tweet

status.getFavoriteCount(); // favorite count

status.getRetweetCount();// retweet count

PAGINATION setMaxId(int) is useful to fetch next set of tweets . this method will  return tweets with status ids less than the given id.


query.setMaxId(lastSeenId);

TWITTER ACTIONS To perform actions like reply retweet you need to authenticate user on twitter. You can find a tutorial on how to authenticate user on twitter using twitter4j here 1] Favorite To perform fav action on any tweet  you  can use createFavorite() method with tweet id.


twitterInstance.createFavorite(mTweetId)

2] Re-tweet To re-tweet tweet  use  retweetStatus() method with tweet id.


twitterInstance.retweetStatus(mTweetId)

3] Reply To reply on a tweet use updateStatus() method with tweet id.


Twitter twitter = TwitterHelper.getTwitterInstance(mContext);

StatusUpdate statusUpdate = new StatusUpdate(mReplyText);

statusUpdate.inReplyToStatusId(mTweetId);

twitter.updateStatus(statusUpdate);

4] Un-favorite To un-favorite  any tweet  you  can use destroyFavorite() method with tweet id


twitterInstance.destroyFavorite(mTweetId)

Just follow the sample, and you’re ready to tweet. Any issues feel free to contact us. You can download working project from github. Happy Coding Happy Learning 🙂

Tutorial on uploading image on twitter using twitter4j

This tutorial walks you through the steps to post a tweet or a tweet with a image on twitter.

First and foremost you should have a valid twitter app configured correctly to post on twitter.

In case you don’t have an app on twitter, follow the steps below to create a simple twitter app and how to configure it correctly.

Step 1.  Goto https://dev.twitter.com/apps Click on create new application button.

Then you will be presented the following page :

1_twit_create_app

Step 1

Fill in the details of your app.

Make sure you don’t leave the CallBack URL blank. A callback url is the one where your app will be redirected to from twitter; this is a test app so for time being you can specify something like eg: http://www.google.com . If you leave this field blank then your app wont be authorized.

Step 2. Now its time to configure your app. Click on the settings tab. Scroll down to the

Application type section and change access to read and write.

2_twit_settings_app

Step 2

Click on the update settings button at the bottom to reflect the changes. Ideally it takes 10 – 15 seconds for the settings to get updated so be patient.

Step 3. Download twitter4J library from the following url http://twitter4j.org/en/index.html and add it to your libs folder.

Note: The zip contains other additional jars. From these we need only twitter4j-core and

twitter4j-media support jars.

3_twit_libs_eclipse

Step 3

Step 4. Now lets get back to some coding. First you need to add the twitter consumer key and secret key to the Strings xml:

<string name = "twitter_consumer_key">YOUR_CONSUMER_KEY</string>
<string name= "twitter_consumer_secret">YOUR_CONSUMER_SECRET</string>
Step 4

Step 4

In our activity we have two buttons:

  • To post a tweet.

  • To post a tweet with image.

On button click we will check whether the app is already authorized if not we will authorize the app using a custom web view.

We will be using the custom webview to authorize the app so that once the app is authorized, you can finish the webview else you will be stuck with a common issue where the view remains opened even after the app is authorized.

Step 5

Step 5

 

The authorization logic is written in LoginActivity class.

Step 5. How to post a tweet/image on twitter ? Is this that tricky ? Lets have a look at it.

a. Post:

 ConfigurationBuilder configurationBuilder = new ConfigurationBuilder();

 configurationBuilder.setOAuthConsumerKey(context.getResources().getString(R.string.twitter_consumer_key));

 configurationBuilder.setOAuthConsumerSecret(context.getResources().getString(R.string.twitter_consumer_secret));

 configurationBuilder.setOAuthAccessToken(LoginActivity.getAccessToken((context)));

 configurationBuilder.setOAuthAccessTokenSecret(LoginActivity.getAccessTokenSecret(context));

 Configuration configuration = configurationBuilder.build();

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

 twitter.updateStatus(message);

b. Post with image:

ConfigurationBuilder configurationBuilder = new ConfigurationBuilder();

configurationBuilder.setOAuthConsumerKey(context.getResources().getString(R.string.twitter_consumer_key));

configurationBuilder.setOAuthConsumerSecret(context.getResources().getString(R.string.twitter_consumer_secret));

configurationBuilder.setOAuthAccessToken(LoginActivity.getAccessToken((context)));

configurationBuilder.setOAuthAccessTokenSecret(LoginActivity.getAccessTokenSecret(context));

Configuration configuration = configurationBuilder.build();

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

StatusUpdate status = new StatusUpdate(message);

status.setMedia(file); // set the image to be uploaded here.

twitter.updateStatus(status);

As you can see posting is as simple as it can be. Further you can download live and functioning app.

Happy Coding Happy Learning 🙂