Java Reference
In-Depth Information
To run the WebReader application, specify a URL as the only command-line argument.
For example:
java WebReader http://www.rssboard.org/rss-feed
Any URL can be chosen; try http://tycho.usno.navy.mil/cgi-bin/timer.pl for the U.S.
Naval Observatory timekeeping site or http://random.yahoo.com/bin/ryl for a random
link from the Yahoo! directory. The preceding example loads a page from an RSS file, as
shown in Figure 17.1.
FIGURE 17.1
Running the
WebReader appli-
cation.
17
Two thirds of the WebReader class is devoted to running the application, creating the user
interface, and creating a valid URL object. The web document is loaded over a stream and
displayed in a text area in the getData() method.
Four objects are used: URL , HttpURLConnection , InputStreamReader , and
BufferedReader objects . These objects work together to pull the data from the Internet
to the Java application. In addition, two objects are created to hold the data when it
arrives: a String and a StringBuffer .
Lines 24-26 open an HTTP URL connection, which is necessary to get an input stream
from that connection.
Lines 27-28 use the connection's getContent() method to create a new input stream
reader. The method returns an input stream representing the connection to the URL.
Line 29 uses that input stream reader to create a new buffered input stream reader—a
BufferedReader object called buff .
After you have this buffered reader, you can use its readLine() method to read a line of
text from the input stream. The buffered reader puts characters in a buffer as they arrive
and pulls them out of the buffer when requested.
Search WWH ::




Custom Search