After a long time I am back in the blogging world. Because I hope French and Mexican people will read this blog I have to write in English...
Since some days ago I am using twitter again (my id is
yannart) but I have a big problem. From work I can't access to the main domain
twitter.com. I try a personal proxy I have installed in a (secret) Internet URL and it works with some bugs. After that I start to wonder how difficult will be coding my own web app with server-side communication against twitter.
Java is my favorite programming language so I search if there where some APIs making easy the communication between a Java program and the twitter servers. I
found 3:
But which one is better?
It's a difficult question I can't respond. However I chose Twitter4J for some simple reasons:
- Good documentation
- Asynchronous support
- IN THE OFFICIAL MAVEN REPOSITORY
Let's code a simple application!Usually I use
Eclipse or
NetBeans IDEs but I this example I will use only Maven 2 so everybody can adapt it in his favorite IDEs (using the official plug-in in NetBeans o the m2eclipse plug-in in Eclipse).
So, that's what you need:
Creation of the project with MavenIn the console run this:
mvn archetype:create \
-DarchetypeGroupId=org.apache.maven.archetypes \
-DgroupId=org.yannart.twitter \
-DartifactId=ConsoleTwitter
Maven will create the project in a folder named ConsoleTwitter.
Adding the dependenciesOpen the folder of the project and edit the pom.xml file:
Add the Twitter4J dependency between the <dependencies> and </dependencies> tags:
...
<dependency>
<groupId>net.homeip.yusuke</groupId>
<artifactId>twitter4j</artifactId>
<version>1.0.6</version>
</dependency>
...
We need to configure the support for Java 5 because the default in Maven is Java 1.4 (for now):
Add this code between the <project> and </project> tags:
...
<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.0.2</version>
<configuration>
<source>1.5</source>
<target>1.5</target>
</configuration>
</plugin>
</plugins>
</build>
...
Modify the main class to retrieve data from twitterGo to the folder src/main/java/org/yannart/twitter/ and open the file App.java.
Replace the existing code in the main method with that one:
public static void main( String[] args ){
if(args.length != 2){
System.out.println("ERROR - you must specify a twitter username and a password.");
System.out.println("Usage: [username] [password]");
return;
}
try {
Twitter twitter = new Twitter(args[0], args[1]);
twitter.update("Updating Twitter from my own Java code!");
List statuses = twitter.getUserTimeline();
System.out.println(" -- " + args[0].toUpperCase() + " timeline --");
//prints each status of the public user timeline
for (Status status : statuses) {
System.out.println(" - " + status.getText());
}
} catch (TwitterException ex) {
ex.printStackTrace();
}
}
This method will connect to Twitter using your username/password and will send the message "Updating Twitter from my own Java code!" and print your user timeline.
Compile the projectIn the console go to the ConsoleTwitter folder and execute the command:
mvn install
This will compile your project and create a target folder with the binaries.
Run the projectTo simplify the execution of the example, we use the exec Maven plug-in. Just run the command:
mvn exec:java -Dexec.mainClass="org.yannart.twitter.App" -Dexec.args="USERNAME PASSWORD"
Remplacing USERNAME and PASSWORD with yours.
Downloading the sourcesThis project is ready to execute. Just download it, compile it and run it.