Java Reference
In-Depth Information
22.5
Harvesting information from the web
Let us now extend the application from the previous section to continuously har-
vest information from the web. In our example application we want to display
stock market information, the German stock index DAX. Such information can
be found on many web pages. We use a web page provided by Yahoo. At the time
the topic was prepared the address was:
http://de.finance.yahoo.com/d/quotes.csv
It contains the quotes of the DAX and the 30 stocks included in this index. The web
!
address might change, so one has to check this and change it if necessary. The page
is formatted as so-called comma-separated values (csv). The actual separator,
however, is a semicolon. This format is suited to be imported into spreadsheet
programs. We use this page because it is easier to analyse than HTML. Every line
contains the information on one stock. A line on this page looks like this:
ˆGDAXI;4016,52;1/13/2004;15:58;+20,61;4006,85;4033,67;4003,19;78199616
It contains - separated by semicolons - the name of the index, its recent value,
date and time of the value and information on the change, highest and lowest
values, etc. The page is regularly updated, approximately every 30 seconds. Our
application extracts the recent value from the line; in the above example this is
4016
52. In our application we use a so-called web query which allows the line
containing the DAX information to be extracted. 1 We do not discuss the HTTP
protocol or web queries here. The reader is referred to corresponding manuals
and tutorials which are available on the web.
The application reads this web page every five seconds, extracts the current
DAX value and updates the display. The quotes are displayed as a graph that
is extended to the right. We do not want the display to be blocked while the
application waits five seconds before fetching the next value. Therefore, we define
a thread for accessing the web. This runs in parallel to the main thread which
handles the GUI. Only the thread class MonitorThread is listed below.
The run -method of MonitorThread consists of a while-loop. The condition
for the while-loop is a boolean variable goOn . This can be set to false by call-
ing method stopThread . Then, the next time the while condition is checked
the loop is terminated, which also terminates the thread. In the loop, the
method getOneQuote is called and then the thread pauses five seconds. Method
getOneQuote is a modification of readAndPrintTheURL from the previous section.
It uses a BufferedReader to read the line with the information on the DAX.
The private method getQuoteFromString parses this line and extracts the re-
cent value of the DAX index as a double . This value is then passed to the display
panel.
The application consists of three more classes MonitorData , MonitorPanel
and OnlineMonitor . The first one implements the data model. It provides methods
.
1
If the information is contained in an HTML page, we would have to analyse the page and
write an appropriate parser to extract the desired information.
Search WWH ::




Custom Search