Twitter API with Java

Hi, Since Twitter is one of the most widely used ways to share your status with others, I will use this post to display how to use Java to access the Twitter API.

I used a Java library named "jtwitter" which can be freely downloaded from
"http://www.winterwell.com/software/jtwitter/jtwitter.jar"

Once you download the jar file, add it to your project. Then, it becomes very simple to use the API.

First build a new Twitter object
Twitter twitter = new Twitter("<username>","<password>");

Then set your status to whatever you want
twitter.setStatus("Twittering");

To get a list of your friends, simply use the following LOC
List followers = twitter.getFriends();

If you need to get the user's image, you can do it in the following manner.
1. Get the URL of the image using Twitter API
2. Use Java Classes to download the image and display it

NOTE: The user variable is of type 'Twitter.User'
URL imgURL = user.getProfileImageUrl().toURL();

Using Java methods, you can download the image.
BufferedImage img = ImageIO.read(imgURL);

Now, you can set it to the iconImage property of a label.
label.setIcon(new ImageIcon(img));

See, it's that simple.

Have fun !!!!

Comments

Popular posts from this blog

Encrypt and Decrypt Images Using Java

Build your own Network sniffer

ASP Response.Write newline