
|


|
|
|
Create a Simple Stock Quote Application
Build a simple stock quote application that will prompt a user for a stock symbol, then perform an HTTP request to the Yahoo! stock quote server, and finally display the stock price
The Code
[Discuss (1) | Link to this hack] |
The Code
import java.io.*;
import javax.microedition.io.*;
import net.rim.device.api.ui.*;
import net.rim.device.api.ui.component.*;
import net.rim.device.api.ui.container.*;
public class StockQuotes extends UiApplication {
private EditField symbolField = new EditField("Symbol: ", "rimm");
private RichTextField priceField =
new RichTextField("", Field.READONLY);
public static void main(String[] args) {
StockQuotes theApp = new StockQuotes( );
theApp.enterEventDispatcher( );
}
public StockQuotes( ) {
MainScreen stockScreen = new MainScreen( );
stockScreen.setTitle("Stock Quotes");
stockScreen.add(symbolField);
stockScreen.add(priceField);
stockScreen.addMenuItem(new MenuItem("Get Quote!", 1, 1) {
public void run( ) {
(new Thread(new Runnable( ) {
public void run( ) {
getQuote( );
}
})).start( );
}
});
pushScreen(stockScreen);
}
private void getQuote( ) {
try {
HttpConnection c = (HttpConnection)
Connector.open("http://finance.yahoo.com/d/quotes.csv?s="
+ symbolField.getText( )+"&f=l1");
c.getResponseCode( );
InputStream in = c.openInputStream( );
int i = in.read( );
StringBuffer sb = new StringBuffer( );
while (i!=-1) {
sb.append((char)i);
i = in.read( );
}
priceField.setText("The last ask price for '" +
symbolField.getText( )+"' was $"+sb.toString( ));
}
catch (IOException e) {
priceField.setText("Error: "+e);
}
}
}
Showing messages 1 through 1 of 1.
-
Works in JDE 4.0.2, broken in JDE 4.1.0
2005-12-01 12:55:37
JuneK
[View]
|
Showing messages 1 through 1 of 1.
|
|
O'Reilly Home | Privacy Policy

© 2007 O'Reilly Media, Inc.
Website:
| Customer Service:
| Book issues:
All trademarks and registered trademarks appearing on oreilly.com are the property of their respective owners.
|
|
|