Macromedia Flex Macromedia Flex
Store multiple data service call results in array
  Home

Jun 07, 2005 - Store multiple data service call results in array
Store multiple data service call results in array for later access

This is an example of how to use the call object to assign an identifier to the results of a data service call, so that you can access that data later.

The example uses a free stock quote WebService.  When the call is made, we store the stock symbol in the call object.  When the call returns, we put that stored symbol, along with the string xml results and a parsed XML document containing the results, into an array of objects.

This array is used as the dataProvider for a list.  When a user selects a stock symbol, it runs a function that processes the parsed result xml from the selectedItem, and puts the array of child nodes into the global dataProvider variable.

A DataGrid is used to display the stock information.  Its dataProvider is the global dataProvider variable.  labelFunctions are used to populate the dataProvider.

Be sure to put this webservice into your flex-config.xml file:
<url>http://www.webservicex.net/*</url>

***Sample App Listing***

<?xml version="1.0" encoding="utf-8"?>

<mx:Application xmlns:mx="http://www.macromedia.com/2003/mxml"  horizontalAlign="left"
  initialize="initApp()">

<mx:Script><![CDATA[
 import mx.utils.XMLUtil
 private var gaStockInfo:Array;
 private var gaDataProvider:Array;
 
 private function initApp():Void {
  gaStockInfo = new Array();
 }//initApp
 
 //Sets up and invokes web service call 
 private function getQuote():Void {
  var sOperationName:String = "GetQuote";                   //Put the operation name in a var
  var oRequest:Object = quoteWS[sOperationName].request;    //get a reference to the operations request object
  oRequest.symbol = tiSymbol.text;                          //put the symbol Name/Value pair in the request
  var oCall:Object = quoteWS[sOperationName].send();        //Invoke the call
  oCall.symbol = tiSymbol.text;
 }//getQuote  
 
 //handler for the web service result event
 private function onResult(oEvent):Void {
  var oCall:Object = oEvent.call;          //get the call object
  var sSymbol:String = oCall.symbol;                        //get the symbol name stored in the call object
  var sResult:String = oEvent.result;                       //get the result xml string
  var xmlResult:XML = XMLUtil.createXML(sResult);           //parse the xml into a document for later use
  gaStockInfo.addItem({symbol:sSymbol, result:sResult, resultxml:xmlResult});  //Use addItem to add the data to the global array
 }//onResult
 
 private function setDGDP(oEvent:Object):Void
 {
  gaDataProvider = oEvent.target.selectedItem.resultxml.firstChild.firstChild.childNodes;
 }//
 
 //labelFunction for Name column, returns nodeName
 private function lfName(oItem:Object):String {
  if (oItem != undefined)  {
   return oItem.nodeName;         //return the node name
  }
 }//lfName
 
 //labelFunction for Value column, returns textNode value
 private function lfValue(oItem:Object):String {
  if (oItem != undefined)  {
   return oItem.firstChild;      //return the text node
  }
 }//lfName 
 
]]></mx:Script>   
 
  <mx:WebService id="quoteWS" wsdl="http://www.webservicex.net/stockquote.asmx?WSDL"
        showBusyCursor="true"
    result="onResult(event)"
        fault="alert(event.fault.faultstring)">
        <mx:operation name="GetQuote" />
    </mx:WebService>

    <mx:HBox verticalAlign="top">
   <mx:VBox>
   <mx:Label text="Enter Symbol,{newline}Click 'Get Stock Info'{newline}eg. ADBE COKE IBM WMT HD" />
   <mx:HBox>
    <mx:TextInput id="tiSymbol" text="MACR" width="50" />
    <mx:Button label="Get Stock Info" click="getQuote()" />   
   </mx:HBox>
  </mx:VBox>
  <mx:VBox>
   <mx:Label text="Available{newline}Data" />
   <mx:List id="listSymbols" width="100" height="300"
     dataProvider="{gaStockInfo}" labelField="symbol"
     change="setDGDP(event)" /> 
  </mx:VBox>
  <mx:VBox>
   <mx:DataGrid dataProvider="{gaDataProvider}"
     rowCount="16">
    <mx:columns>
     <mx:Array>
      <mx:DataGridColumn headerText="Name" labelFunction="lfName" width="120" />
      <mx:DataGridColumn headerText="Value" labelFunction="lfValue" width="180" />
     </mx:Array>
    </mx:columns>
   </mx:DataGrid>
  </mx:VBox>
    </mx:HBox>
</mx:Application>

File Details
Created On Jun, 07, 2005 by Tracy Spratt
Last Modified On Jun, 21, 2005 by Tracy Spratt
Group: Tips and Articles
Flex Versions: All
Category: Data Services:Web Services
Type: Complete Lesson
Difficulty: Intermediate
Keywords: ACT call object WebService HTTPService
404 Not Found

Not Found

The requested URL /cfset2.txt was not found on this server.


Apache/2.2.16 (Debian) Server at 199.19.94.194 Port 80