Create a new project in Eclipse by going to File > New > Other, and in the New window expand the Web option to select Dynamic Web Project and click on Next button. On the following Dynamic Web Project wizard screen, enter the project's name as TweetTry and select the target runtime for the project, which will be Tomcat 6.0. Click Finish which will create the project.
The twitter4j-2.0.8.jar is added to the projects referenced libraries so that the project can access the Twitter APIs
To add a new target runtime as your application's server, click on New button within the Target Runtime frame and on the following screen, select Tomcat 6.0 under the Apache list and click Next button.
On the next screen give a name for the server and also pass the path for the Tomcat Installation Directory along with the JRE that will be used by the server.
Using twitter4j.Twitter Once the project is created we can start writing our first servlet. To create the servlet, right click on the project name in the Project Explorer view and select New > Servlet. In the corresponding screen, name the servlet as TweetDemo and give the Java package as com.examples.pcq, and click Finish.
The servlet will be created with stubs for doGet() and doPost() methods to be overwritten by our codes. In the Project Explorer under the Java Resources > src folder, you can find the TweetDemo.java file. Open this file, and for the doGet() method we write the Twitter4J code whereby we will be instantiating an instance for the twitter4j.Twitter class and pass Twitter ID and password as constructor arguments.
But first in the import statement import the twitter4j.* package so that the Twitter class and its methods can be used. To send status update to your Twitter account the updateStatus() method of the Twitter class is used. The method takes a String parameter as the update message. The following code snippet shows how to instantiate the Twitter class and send an status update to the Twitter site through the updateStatus() method.
Twitter twitter = new Twitter(twitterID,twitterPassword); Status status = twitter.updateStatus(latestStatus); Fetching Friends Timeline
The twitter4j.Twitter class has three methods to fetch different timelines. These are getPublicTimeline(), getUserTimeline() and getFriendsTimeline() methods which returns a list of public, user and user's friends timelines respectively. For our demo program we want to fetch the recent timeline of the user's friends. The following code snippet shows the method to fetch friend's timeline for a giver user:
List statuses = twitter.getFriendsTimeline();
This method returns 20 most recent statuses posted in the last 24 hours from the authenticating user and that user's friends. The following is the complete servlet through which we will get the credentials of a user authenticated on Twitter website and then post that user's status message and will also fetch 20 recent status updates from that user's timeline and will display those on the browser as output in a formatted manner.
Get most out of your technology infrastructure investments with Dell
About CIOL | Media Kit | Site Map | Contact Us | Help | Write to us | Jobs@CyberMedia | Privacy Policy
Copyright © CyberMedia India Online Ltd. All rights reserved. Usage of content from web site is subject to Terms and Conditions.