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: Java5 or later
How To Use
It's as simple as adding twitter4j-1.1.2.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 (DirectMessage 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"); } } } );
Download

twitter4j-1.1.2.zip
Source Code
The archive contains jar file along with source code.
You can browse the project repository at http://yusuke.homeip.net/fisheye/browse/svn/twitter4j/trunk
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
Maven Integration
You can integrate Twitter4j easily by including following lines into your pom.xml.
<dependencies>
<dependency>
<groupId>net.homeip.yusuke</groupId>
<artifactId>twitter4j</artifactId>
<version>[1.1,)</version>
</dependency>
...
</dependencies>
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.Copyright (c) 2007-2008, 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
- [TFJ-61] - AsyncTwitter#createFavoriteAsync(), destoryStatusAsync(), destroyFavoriteAsync() should take long instead of int
- [TFJ-64] - AsyncTwitter#show(long) fails with ClassCastException
- [TFJ-63] - typo: detroy -> destory
- Release Notes - Twitter4J - Version 1.1.1
- [TFJ-54] - Show method should take long instead of int
- [TFJ-55] - Show method shouldn't require userid and password
- [TFJ-56] - TwitterResponse from AsyncTwitter is not Serializable
- [TFJ-57] - Date is encoded twice, and therefore "since" doesn't work
- [TFJ-60] - hashCode and equals are not properly implemented
- [TFJ-47] - implement toString()
- Version 1.1.0
- [TFJ-44] - #document: null] is unnecessarily printed to the System.out
- [TFJ-31] - dereference DOM objects to reduce memory footprint
- [TFJ-34] - Status#getCreatedAt(), UserWithStatus.getCreatedAt() and DirectMessage.getCreatedAt() should return java.util.Date
- [TFJ-41] - Status.getId(), UserWithStatus.getStatusId() should return long instead of int
- [TFJ-43] - remove unsupported methods
- [TFJ-45] - getFollowers() should accept id and/or page parameter
- Version 1.0.6
- [TFJ-38] - getDirectMessage(int since) fails with 500 Internal Server Error
- [TFJ-37] - Unexpected root node name:nilclasses. Expected:nil-classes
- [TFJ-42] - maximum number of count is now 200
- Version 1.0.5
Resolved Bugs
- [TFJ-36] - create/destroyFavorite, follow, leave, block, unblock fail with HTTP 400 code
New Feature
- [TFJ-35] - add destory method for status
- Version 1.0.4
- [TFJ-22] - pom.xml does not produce source jar file
- [TFJ-27] - add "source", "truncated", "in_reply_to_status_id", "in_reply_to_user_id", "favorited" to Status, "followers_count" to User
- [TFJ-21] - ability to force using POST method
- [TFJ-24] - support new APIs introduced on April 29th, 2008
- [TFJ-25] - ability to change user id and password
- [TFJ-26] - ability to dump request and response
- Version 1.0.3
- [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