Accessibility

Table of Contents

So You're Thinking About Using Flash Instead of J2ME?

Sending the Request for the Stock Quote

So far you have created a simple user interface and discovered how to work with variables and persistent storage. Now it is time to add some substance to the application by sending a request for a stock quote using the symbol entered by the user to a server over HTTP.

After some searching and help from another Flash Lite developer, I eventually found out how to open a URL and process the result. You use an object called LoadVars, as shown in the following example:

var lv:LoadVars = new LoadVars();
lv.owner = this;
lv.onData = function(str) {
    this.owner.parseResult(str);
};
lv.load("http://quote.yahoo.com/d/quotes.csv?s="+symbol+"&f=slc1wop");

The data is passed to my function parseResult(str), which is defined as follows:

function parseResult(quote:String){
   trace(quote);
   // Split the quote up into the individual fields
   var fields:Array = quote.split(",");
   // Get the last trade time and value 
   var lastValues:String = fields[1].substring(1, fields[1].length-1);
   var timeAndValue:Array = lastValues.split("-");
   this.lastTradeTime = timeAndValue[0];
   var s:String = timeAndValue[1].substring(4, timeAndValue[1].length-4);
   this.lastTradeValue = parseFloat(s);
   // Get the change in value
   this.changeInValue = parseFloat(fields[2]);
   // Get today's hi -------------------------------------
   // Remove the quotes
   s = fields[3].substring(1, fields[3].length-1);
   var todaysHiAndLo:Array = s.split("-");
   this.loValue = parseFloat(todaysHiAndLo[0]);
   this.hiValue = parseFloat(todaysHiAndLo[1]);
   this.openValue = parseFloat(fields[4]);
   this.prevValue = parseFloat(fields[5]);
}

Note: I created the class Stock to encapsulate a stock. See the file stock.as for more details.

I use the string functions to pick out the bits I want and assign them to the member variables lastTradeTime, lastTradeValue, and so on in the Stock class. I've hooked these values up to the Dynamic Text fields in Frame 8. When I go to Frame 8, the values are displayed automatically.

Note: Flash offers object-orientated features such as interfaces, inheritance, and encapsulation. It uses a new operator to create objects. For more details, within Flash Professional select Help > Flash Help > Learning ActionScript 2.0 in Flash > Classes > About Object-Oriented Programming and Flash > Object-Oriented Programming Fundamentals (also available in the Flash LiveDocs).

Working with Timer Intervals

If you take a look at the ActionScript associated with Frame 1, you will see an example of how to use a timer event. The code sets an interval of 4000 milliseconds on which it calls the function showMenu(). To stop the interval, use the method clearInterval with the ID returned by the function setInterval().