红联Linux门户
Linux帮助

Write your own Twitter application

发布时间:2009-03-31 14:18:41来源:红联作者:jerry520
That buzz you've been hearing is the sound of millions of Twitter updates -- tweets -- careening around cyberspace. Even cooler than the Twitter social-networking service itself is that fact that Twitter data is exposed in an open API that your applications can tap into. With iText creator Bruno Lowagie as your guide, find out how to leverage three open source Java libraries to archive tweets dynamically in a PDF document. You'll write standalone Java code, then integrate it into a servlet so that you can offer the application as a service to other Twitter users.

The Twitter social-networking and micro-blogging service has become immensely popular since it launched in 2006. Twitter users send tweets -- real-time, text-based updates of up to 140 characters -- and read other users' tweets via the Twitter Website, Short Message Service (SMS), Really Simple Syndication (RSS), or Twitter applications. Tweets are displayed on the user's profile page and delivered to other users who have signed up to receive them. In this article you'll learn how to build your own Twitter service: an application that accesses tweets via the Twitter API and archives them in the form of a PDF file.

You'll build your application with the help of three open source Java libraries:

* HttpClient 3.x from the Apache Commons library. You'll use this API to obtain an XML stream of tweets from the Twitter API, as well as the Commons Logging and Codec components.
* dom4j to parse the XML and extract specific data from each tweet.
* iText to create the PDF document.

This is a hands-on tutorial, so download those libraries if you haven't already done so. I'll remind you which JARs you need from them as we go along. If you don't already have a Twitter account, set one up now and start using the service so that you have some tweets to work with.

You'll begin by writing your Twitter application as a standalone Java program. Eventually you'll integrate the code in a servlet, so that you can offer the application on your site as a service available to other Twitterati.
Getting started

Every Twitter application makes use of the Twitter API, which is well documented on the Twitter API wiki. Let's suppose you're just interested in read-only access for now: you want to retrieve tweets and visualize them in some way. You don't know yet that you're going to produce a PDF document; you only know that you're going to "consume" a number of tweets. That's why you start by writing an interface for such a consumer, as shown in Listing 1.
文章评论

共有 0 条评论