Introduction
Twitter4J is a Java library for TwitterAPI.
With Twitter4J, you can easily integrate your application with the Twitter service.
System Requirements
OS: Windows or any flavor of Unix that supports Java.
JVM: JDK1.4.2 or later
How To Use
It's as simple as adding twitter4j-1.0.3.jar to your classpath.
If you are familiar with Java language, the JavaDoc should be Obviously the shortest way for you to understand Twitter4J.
twitter4j.Twitter class is the one you may want to look first.
Code Samples
Sample codes are located at src/twitter4j/examples/ and you can run each classs using bin/className.cmd|sh.
- Updating status
- Getting Timeline
- Sending / Receiving Direct Messages
- Asynchronous API
You can update "What are you doing?" via Twitter.update() method.
See also twitter4j.examples.Update.java for detail.
Twitter twitter = new Twitter(twitterID,twitterPassword); Status status = twitter.update(latestStatus); System.out.println("Successfully updated the status to [" + status.getText() + "].");
Twitter.get****Timeline() returns a List of public, friends or specified user's timeline.
See also twitter4j.examples.GetTimelines.java for detail.
Twitter twitter = new Twitter(twitterID,twitterPassword); statuses = twitter.getFriendsTimeline(); System.out.println("Showing friends timeline."); for (Status status : statuses) { System.out.println(status.getUser().getName() + ":" + status.getText()); }
You can send and receive direct messages via Twitter.sendDirectMessage() / Twitter.getDirectMessages().
See also twitter4j.examples.Get/SendDirectMessage(s).java for detail.
Twitter twitter = new Twitter(senderID,senderPassword); sender.sendDirectMessage(recipientId,message); Twitter receiver = new Twitter(recipientId,recipientPassword); Listmessages = receiver.getDirectMessages(); for (DireceMessage message : messages) { System.out.println("Sender:" + message.getSenderScreenName()); System.out.println("Text:" + message.getText() + "\n"); }
It is possible to call those time consuming TwitterAPI asynchronously using twitter4j.AsyncTwitter class along with TwitterListener.
Actual method calls will be done in a separate thread and you can get the responses through TwitterListener interface.
See also twitter4j.examples.AsyncUpdate.java for detail.
AsyncTwitter twitter = new AsyncTwitter(senderId,senderPassword); twitter.updateAsync(args[2], new TwitterAdapter() { @Override public void updated(Status status) { System.out.println("Successfully updated the status to [" + status.getText() + "]."); synchronized (lock) { lock.notify(); } } @Override public void onException(TwitterException e, int method) { if (method == UPDATE) { e.printStackTrace(); synchronized (lock) { lock.notify(); } } else { synchronized (lock) { lock.notify(); } throw new AssertionError("Should not happen"); } } } );
Source Code
The archive contains jar file along with source code.
You can access the project repository via http://yusuke.homeip.net/svnwebclient/directoryContent.jsp?url=twitter4j
Or you can check out the latest source code anonymously with a subversion client as follows:
svn checkout http://yusuke.homeip.net/svn/twitter4j/trunk twitter4j
Mailing list
Any comment, bug report, feature request or patch is highly welcomed.
Feel free to post your comments to the mailing list.
![]() |
Subscribe to Twitter4J |
Visit this group |
Known bugs and feature requests will be filed to the Jira site.
Go to Jira
License
Twitter4J is released under a BSD-style license.Twitter4J bundles Apache Commons Codec which is released under the Apache Software License .
Copyright (c) 2007, Yusuke Yamamoto All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. * Neither the name of the Yusuke Yamamoto nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY Yusuke Yamamoto ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL Yusuke Yamamoto BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Pricing
Twitter4J is an opensourced software and free of charge.You can use Twitter4J freely for any commercial or non-commercial project.
If you feel Twitter4J makes your life easier, please do consider making a donation!
Vesion History 
- Version 1.0.3 new!
- [TFJ-16] - support for follow / leave methods
- [TFJ-17] - ability to specify "page" to getFriends() method
- [TFJ-18] - support for Account Methods
- [TFJ-19] - support for favorite methods
- Version 1.0.2
- [TFJ-9] - remove Commons-Codec dependency
- [TFJ-8] - ability to set "source" parameter and request headers
- [TFJ-10] - support "sender" and "recipient" elements in direct messages
- [TFJ-11] - support for "sent" API
- [TFJ-12] - support for pagination parameter
- [TFJ-15] - ability to pass since_id to getDirectMessges()
- Version 1.0.1
- [TFJ-2] - Twitter#getUserDetail() throws NullPointerException
- [TFJ-3] - some methods of User instance retrieved from Twitter#getFriends() throw NullPointerException
- [TFJ-4] - getDirectMessages() throws TwitterException when response contains no messages
- [TFJ-5] - Twitter#getReplies() throws TwitterException
- [TFJ-6] - NullPointerException throwed during Twitter service maintenance
- [TFJ-1] - need support for destroying directmessage
- Version 1.0.0 Released Initial Version