Build Your Own Social Networking Interface With Twitter4J
Join the DZone community and get the full member experience.Join For Free
With all the buzz around Twitter these days, it's no surprise that we see new clients popping up everywhere - on our desktops, the web and as mobile applications. While some applications are done quite well, it can be difficult to find the one that has the features that you want. But like all software developers, you have a choice - you can go ahead and build one of your own. That's where Twitter4J fits in.
I first became aware of Twitter4J as I was working on my Twitter client
using ECF. No matter what feature I needed from Twitter, it was
supported in Twitter4J. Of course, you can integrate into any Java
application from version 1.4.2, and there's also support for the
Android platform. In this article i'm going to give a brief overview of
how to use the framework to connect to, and use, Twitter. With the
basic building blocks that the library provides, you can create your
own rich Twitter interface.
It couldn't be easier to get going with this library - simply download the latest version, and add the core library to your classpath.
To connect up to your twitter account, you simply create a new instance of a Twitter object, using your real username and password.
Twitter twitter = new TwitterFactory().getInstance("username", "password");
Twitter4J also has OAuth support, which can be used as outlined at the project code sample page.
Updating your status from the API is a simple one-liner (not including exception handling):
Status status = twitter.updateStatus("Test message from twitter4j");
catch (TwitterException e)
Finding out what everyone else has been saying works by getting your friends timeline, which is just a list of Status objects.
List<Status> statuses = twitter.getFriendsTimeline();
System.out.println("Friend Status Updates");
for (Status status : statuses)
System.out.println(status.getUser().getName() + " said " + status.getText());
The standard getFriendsTimeline call will return the last 20 messages. But there is a paging concept, which gives you access to the amount of messages that you want at a particular time. This example gets the last 100 messages:
Paging page = new Paging(1, 100);
List<Status> statuses = twitter.getFriendsTimeline(page);
The Status object contains all information that you will need to know about that update such as the time it was created at, the geographic location, the user who performed the update, and whether it was in reply to another message.
Meanwhile the User object gives you access to all of a typical Twitter users public information, including their avatar.
I really like how Twitter.com show messages timestamps in the form of "3 minutes ago", rather than a plain old date. I found the PrettyTime library, which provides all you need to do this in Java.
PrettyTime p = new PrettyTime();
Watch Out For...
As you're developing, you'll probably run into "Rate Limited Exceeded" messages. This means that you've used more than the 150 hourly API calls that are allowed. If you plan on developing further, you can request for a higher limit from Twitter. However, the rate limit being lifted only applies to your ID, so it's recommended that you cache your Twitter API calls.
There's lots more available in the Twitter4J library - anything that you can think of that you might need in your Twitter application is supported. We may have enough dedicated Twitter applications, but if you want to integrate Twitter support into an existing application, or you want to write an Android app that lets you do what you want on Twitter, there's no excuse not to get started.
Opinions expressed by DZone contributors are their own.
Front-End: Cache Strategies You Should Know
How to Use an Anti-Corruption Layer Pattern for Improved Microservices Communication
A Complete Guide to Agile Software Development
Future of Software Development: Generative AI Augmenting Roles and Unlocking Co-Innovation