Program Resource

Resource libraries for programmers and developers

Simple code to get specified user’s tweet in Android, v1.1 support.
API has restriction, where tweet data can be fetched up to 15 times per 15 min from same IP.

Reference:
http://stackoverflow.com/questions/12916539/simplest-php-example-retrieving-user-timeline-with-twitter-api-version-1-1

Twitter4j library is used; download from site below and copy required jar file to your libs folder in project.

Tweeter4j
http://twitter4j.org/en/index.html

Next, you need to get a access key.

https://dev.twitter.com/apps

Login, “Create a new application” and go to OAuth Settings to get your key.

Copy paste your 4 keys to code, and you are done.

public void init_fetch_tweet(String tweet_user_id,int tweet_num) {

	try {
   		String strConsumerKey = "*********";
   		String strConsumerSecret = "*********";
   		String strAccessToken = "*********";
   		String strAccessTokenSecret = "*********";

   		ConfigurationBuilder confbuilder  = new ConfigurationBuilder();
   		confbuilder.setOAuthConsumerKey(strConsumerKey);
   		confbuilder.setOAuthConsumerSecret(strConsumerSecret);
   		confbuilder.setOAuthAccessToken(strAccessToken);
   		confbuilder.setOAuthAccessTokenSecret(strAccessTokenSecret);

   		Twitter twitter = new TwitterFactory(confbuilder.build()).getInstance();
   		List<Status> statuses = twitter.getUserTimeline(tweet_user_id,new Paging(1,tweet_num));
   		int count=0;
   		Collections.reverse(statuses); //reverse order
   		for (Status status : statuses){
       		String tweettext = status.getUser().getName()+&quot;:&quot;+status.getText()+&quot;\n (&quot;+status.getCreatedAt().toLocaleString()+&quot;)&quot;;
       		//now single tweet is in tweettext
   		}
	catch(Exception ex){
		//failed to fetch tweet
	}
}
Print Friendly, PDF & Email

This post is also available in: Japanese

Leave a Reply

Your email address will not be published. Required fields are marked *


*

CAPTCHA