Hack 95. 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.

To create BlackBerry applications, you must download and install the Black-Berry Java Development Environment (JDE) from the http://www.blackberry.com web site. The JDE contains the development environment, compiler, MDS Simulator, and the BlackBerry Device simulator [Hack #93]. Once the JDE is installed, you are ready to start writing your application.

Launch the JDE, and create a new workspace and a project and call it whatever you want. Then add a new .java file to your project. The simplest version of a BlackBerry UI application must simply contain a public static void main(String[] args) method. Here is an example of the simplest BlackBerry application:

 import net.rim.device.api.ui.*; public class StockQuotes extends UIApplication { public static void main(String[] args) { } } 

This application does not do anything it does not even display a user interface. The next step is to create a user interface.

For this stock quote example, we will simply show a text input field in which the user can enter the stock symbol:

 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 { public static void main(String[] args) { StockQuotes theApp = new StockQuotes(); theApp.enterEventDispatcher(); } public StockQuotes( ) { MainScreen stockScreen = new MainScreen( ); stockScreen.setTitle("Stock Quotes"); stockScreen.add(new EditField("Symbol: ", "rim.to")); pushScreen(stockScreen); } } 

When the application is run, a simple interface is displayed to the user, as shown in Figure 9-2.

Figure 9-2. Stock Quotes main screen


The next step is to add a menu item. When clicked, this menu item will perform an HTTP connection to the Yahoo! Finance stock quote server:

 stockScreen.addMenuItem(new MenuItem("Get Quote!", 1, 1) { public void run( ) { // place event handler code here } }); 

Now in the event handler code, simply add the necessary HTTP request code [Hack #94]. When the menu item is clicked, the stock quote is fetched and the result displays, as shown in Figure 9-3.

Figure 9-3. Stock Quotes result


9.3.1. 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); } } } 

9.3.2. Run the Code

Create a new project in the BlackBerry JDE called StockQuotes. Underneath the project, make a new file called StockQuotes.java. Save the previous text into the file, and then go to the Build menu and select Build All and Run. This will compile your code and start the simulator with the application already "installed" on the BlackBerry.

With HTTP requests and simple applications mastered, it is now possible to build any type of networked application such as web browsers or custom applications.

Paul Dumais



BlackBerry Hacks
Blackberry Hacks: Tips & Tools for Your Mobile Office
ISBN: 0596101155
EAN: 2147483647
Year: 2006
Pages: 164
Authors: Dave Mabe

Similar book on Amazon

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net