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.
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.
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()+":"+status.getText()+"\n ("+status.getCreatedAt().toLocaleString()+")"; //now single tweet is in tweettext } catch(Exception ex){ //failed to fetch tweet } }
This post is also available in: Japanese