Post to Twitter using Java: Twitter4J
I have to confess that I’m a fairly recent convert to the social phenomena that is Twitter. Although I joined up over a year ago, it’s only in the last few weeks that I’ve really caught the twitter bug in a big way.
With this in mind it was with interest the other day that I stumbled across a great little api for accessing Twitter thats been implemented in Java: Twitter4J. This is a really neat java port of the Twitter API and is exceptionally easy to use and quick to pick up. The site comes with a number of examples to get you started with and with a well written JavaDoc. The project is open source (the download comes with the source bundled) and is licensed under a BSD style license and appears to be being updated on a regular basis. That’s the fluff done, let’s look at some examples…
Almost all of the code you’ll write using Twitter4J will start with you instantiating an instance of the base class twitter4j.Twitter, normally passing over your Twitter username and password as constructor arguments. This one class contains much of the core functionality that the API provides you to access and manipulate your Twitter account.
(In the following examples I use my twitter username (rdean) and ****** to indicate a password)
1. The basics, sending a message
// instantiate a new Twitter instance
Twitter twitter = new Twitter("rdean", "******");
// now update your status
Status status = this.twitter.update("This is me twittering in java on " + new Date());
Run this code and then check your Twitter account and your status should have been updated. Indeed you can even look at the returned Status object to get more information about your post - such as the Twitter id.
2. Read other user’s tweets
Twitter twitter = new Twitter("rdean", "******");
// get the last 20 posts from people in your friend's list
List statuses = twitter.getFriendsTimeline();
for (Status status : statuses)
{
System.out.println(status.getUser().getName() + ":"
+ status.getText() + " " + status.getSource() + " "
+ status.getCreatedAt());
}
The above returns just the 20 most recent posts from all friends of the logged in user. You can expand on this by calling:
twitter.getFriendsTimeline(String id)
returns the 20 most recent posts for a single user
twitter.getFriendsTimelineByPage(int page)
returns 20 posts from a given page of results (where each page is 20 posts long)
3. Show users
To get all of your current followers you can do:
List followers = twitter.getFollowers();
for (User user : followers)
{
System.out.println(user.getScreenName());
}
or to get a list of another user’s followers:
List followers = twitter.getFollowers("anotheruser");
Replace the ‘Followers’ with ‘Friends’ in the above methods to get just those!
Once you have a User object you gain access to all sorts of interesting information about the User such as their description, profile image url and even if their profile is protected or not. Calling
twitter.getUserDetail(String userid);
will return a UserWithStatus object which provides you with even more information about the user, for example their current status text, status count and friends and followers counts.
I think that’s enough examples for now, there’s loads and loads more functionality lurking in the API which will allow you do almost anything you can think of with Twitter - make friends, block users, send private messages and even do all of the above asynchronously in separate threads. I can think of any number of cool little widgets or applications that could be developed with this in mind. I’d certainly recommend other developers out there who are interested in integrating some kind of Twitter functionality into their applications take a look and have a play.
November 7th, 2008 at 8:45 am
you can get access to Twitter right from your JSP (and/or Coldfusion) pages with this taglib: http://www.servletsuite.com/servlets/twittertag.htm
November 7th, 2008 at 8:53 am
That’s cool Den, thanks for the pointer. I haven’t used JSP in a while but I’ll have a play next time I get the chance!
July 10th, 2009 at 7:26 pm
Hi! Very useful information it Worked.
Just one question: How do I change the “via Twitter4J” stuff when I send an update? I mean if would like to make a custom client it would be cool if it says “via CustomTwitterClient”.
July 24th, 2009 at 4:19 pm
I’m using twitter4j in my Java Bot and when I execute the command to update my status, I get this in my log:
“java.lang.NoClassDefFoundError: twitter4j/TwitterException”
My code is
try {
Twitter twitter = new Twitter(”*****”,”*****”);
// Set my status
twitter.update(”test update message”);
}
catch (TwitterException e) {
}
}
Thanks!
September 9th, 2009 at 4:09 pm
Hi! I was surfing and found your blog post… nice! I love your blog.
Cheers! Sandra. R.