AS3 webserive: load stock data using webservice

[ 2009-08-18 09:50:00 | Author: liuhuan ]
Font Size: Large | Medium | Small
The stockquote webserivce enables us to get real stock data, which we can use in flash to create a stock information application.

WS url: http://www.webservicex.net/stockquote.asmx



Here we explore how to access webservice in Actionscript 3.0:
Quote
package {
  import flash.display.*;
  import flash.events.*;
  import flash.net.*;
  import fl.controls.ComboBox;
  import flash.text.TextField;
  import flash.text.TextFieldAutoSize;

  public class Main extends Sprite {
    private var tf:TextField;
    private var cb:ComboBox;
    private var myData:Object = new Object();
    public function Main():void {
      cb = new ComboBox();
      cb.prompt = "Select a company Symbol for Stock quote";
      cb.addItem( { label: "Johnson & Johnson", data:"JNJ" } );
      cb.addItem( { label: "Procter & Gamble", data:"PG" } );
      cb.addItem( { label: "Best Buy", data:"BBY" } );
      cb.addItem( { label: "Coca-Cola", data:"KO" } );
      cb.addItem( { label: "Merck & Co., Inc.", data:"MRK" } );
      cb.addItem( { label: "Nestle SA Reg Shs. Ser. B Spons", data:"NSRGY" } );
      cb.addItem( { label: "NIKE", data:"NKE" } );
      cb.addEventListener(Event.CHANGE, onSelected);
      cb.width = 300;
      cb.move(10, 10);
      addChild(cb);
      tf = new TextField();
      tf.x = 10;
      tf.y = 60;
      tf.width = 500;
      tf.wordWrap = true;
      tf.multiline = true;
      tf.autoSize = TextFieldAutoSize.LEFT;
      addChild(tf);
      tf.htmlText = "Please Select a company Symbol above for Stock quote..."
    }
    private function onSelected(e:Event):void {
      tf.htmlText = "<font size='28'>"+cb.selectedItem.label+"</font><br><br>";
      var request:URLRequest=createRequest();
      URLVariables(request.data).symbol=cb.selectedItem.data;
      var loader:URLLoader=new URLLoader;
      loader.addEventListener(Event.COMPLETE, onComplete);
      loader.addEventListener(IOErrorEvent.IO_ERROR, onError);
      loader.load(request);
    }
    private function onComplete(e:Event):void {
      var xml_source:XML = new XML(e.currentTarget.data);
      var xml:XML = new XML(xml_source.toString());
      var i:int = 0;
      var xmldata:XML;
      myData.stockData = [];
      for each (xmldata in xml.*) {
        trace(xmldata);
        myData.stockData[i] = {};
        myData.stockData[i].Symbol = xmldata.Symbol;
        myData.stockData[i].Last = xmldata.Last;
        myData.stockData[i].Date = xmldata.Date;
        myData.stockData[i].Time = xmldata.Time;
        myData.stockData[i].Change = xmldata.Change;
        myData.stockData[i].Open = xmldata.Open;
        myData.stockData[i].High = xmldata.High;
        myData.stockData[i].Low = xmldata.Low;
        myData.stockData[i].Volume = xmldata.Volume;
        myData.stockData[i].MktCap = xmldata.MktCap;
        myData.stockData[i].PreviousClose = xmldata.PreviousClose;
        myData.stockData[i].PercentageChange = xmldata.PercentageChange;
        myData.stockData[i].AnnRange = xmldata.AnnRange;
        myData.stockData[i].Earns = xmldata.Earns;
        myData.stockData[i].Name = xmldata.Name;
        i++;
      }
      tf.htmlText += "<b>Symbol:</b>"+myData.stockData[0].Symbol+"<br>";
      tf.htmlText += "<b>Last:</b>"+myData.stockData[0].Last+"<br>";
      tf.htmlText += "<b>Date:</b>"+myData.stockData[0].Date+"<br>";
      tf.htmlText += "<b>Time:</b>"+myData.stockData[0].Time+"<br>";
      tf.htmlText += "<b>Change:</b>"+myData.stockData[0].Change+"<br>";
      tf.htmlText += "<b>Open:</b>"+myData.stockData[0].Open+"<br>";
      tf.htmlText += "<b>High:</b>"+myData.stockData[0].High+"<br>";
      tf.htmlText += "<b>Low:</b>"+myData.stockData[0].Low+"<br>";
      tf.htmlText += "<b>Volume:</b>"+myData.stockData[0].Volume+"<br>";
      tf.htmlText += "<b>MktCap:</b>"+myData.stockData[0].MktCap+"<br>";
      tf.htmlText += "<b>PreviousClose:</b>"+myData.stockData[0].PreviousClose+"<br>";
      tf.htmlText += "<b>PercentageChange:</b>"+myData.stockData[0].PercentageChange+"<br>";
      tf.htmlText += "<b>AnnRange:</b>"+myData.stockData[0].AnnRange+"<br>";
      tf.htmlText += "<b>Earns:</b>"+myData.stockData[0].Earns+"<br>";
      tf.htmlText += "<b>Name:</b>"+myData.stockData[0].Name+"<br>";
    }
    private function onError(e:Event):void {
      trace("error occurs");
    }
    private function createRequest():URLRequest {
      var request:URLRequest=new URLRequest('http://www.webservicex.net/stockquote.asmx/GetQuote&#39;);
      var variables:URLVariables=new URLVariables;
      request.data=variables;
      return request;
    }
  }
}
Endpoint

http://www.webservicex.net/stockquote.asmx

Disco

http://www.webservicex.net/stockquote.asmx?Disco

WSDL Location

http://www.webservicex.net/stockquote.asmx?wsdl
Comments Feed Comments Feed: http://www.liuhuan.com/blog/feed.asp?q=comment&id=1012

There is no comment on this article.

You can't post comment on this article.