2 May 2011
Beginning
The Flex framework supports the Simple Object Access Protocol (SOAP) specification for web services through the WebServices component. The WebServices component can consume data from the server in the structured XML format named Web Services Description Language (WSDL).
In this exercise you will use the same form from Exercise 2.04 (Populating an ArrayCollection with retrieved data) to implement a WebService object that handles the WSDL document with dynamic data for the application. You will retrieve all of the fictional employees in a company to display in a DropDownList control (see Figure 1).
In this section, you will use a WebService object to retrieve the data from a WSDL document generated on request to a URL.
Note: This example generates the XML using an Adobe ColdFusion server, but you can use any server to generate the XML file. Refer to the Flex and ColdFusion, PHP, Java, or .NET integration pages for more information.
You should see the WSDL document shown in Figure 2.
getEmployees() method. This is the method you will be using to query the data on the server from Flash Builder (see Figure 3).
Declarations block.Declarations tag set, press CTRL+Space twice to invoke the content assist tool, type We and select the WebService code template (see Figure 4). You should see a WebService object instance added to your code.
<fx:Declarations>
<s:WebService id="service"
wsdl="wsdl"
useProxy="false"
showBusyCursor="true"
result="onResult(event)"
fault="onFault(event)"/>
</fx:Declarations>
id property value to employeeService and the wsdl property value to:http://www.adobetes.com/f45iaw100/remoteData/employeeData.cfc?wsdl.
useProxy, showBusyCursor, result, and fault properties from the code.<s:WebService id="employeeService"
wsdl="http://www.adobetes.com/f45iaw100/remoteData/employeeData.cfc?wsdl"/>
In this section you will initialize the retrieval of data by the WebService object.
initApp() function.employeeService.getEmployees() method.private function initApp():void
{
employeeService.getEmployees();
pickupDate.addEventListener(CalendarLayoutChangeEvent.CHANGE, dateChangeHandler);
returnDate.addEventListener(CalendarLayoutChangeEvent.CHANGE, dateChangeHandler);
}
Note: In order to complete this part of the exercise, you must have Flash Builder Premium Edition installed. If you have Flash Builder Standard Edition installed, skip this section and continue with the next section. If you want to download and install a trial version of Flash Builder Premium Edition, be sure to 1) back up your existing workspace and projects, 2) uninstall Flash Builder Standard Edition, and then 3) install the trial version of Flash Builder Premium Edition. To compare the Standard Edition with the Premium Edition, check out the Upgrade Details page.
In this section you will use the Network Monitor view to examine the data returned by the WebService object.
Note: If the workspace you are using has multiple projects open, you may not see the recording in the Network Monitor. Close all other projects and enable the monitor again for your current project and make sure the message in the Network Monitor view reads, "Enabled for project 'ex2_05_web_starter.'"
You should see that the data result is displayed in the Network Monitor as shown in Figure 8. If you expand the data node of the Response body tree, you should see the data for each employee. If you expand the column List node, you should see all of the data properties. Note the data properties are all uppercase. ColdFusion returns properties in all upper case format by default. You need to adjust the Flex controls to be compatible with the data.
In this section you will store the returned data from the WebService object in an ArrayCollection variable, named employees, that is bound to the DropDownList control in the Employee Portal: Vehicle Request Form.
employeeService_resultHandler() function.event.result property:protected function employeeService_resultHandler(event:ResultEvent):void
{
employees = event.result;
}
as operand to cast the event.result value to the ArrayCollection class.The returned data is typed to the Object class by default. You must cast the result to the ArrayCollection type so that it can be used with the employees ArrayCollection variable.
protected function employeeService_resultHandler(event:ResultEvent):void
{
employees = event.result as ArrayCollection;
}
In this section you will adjust the application user interface to use the data returned by the WebService object.
labelField property to LASTNAME:<s:FormItem label="Employee:">
<s:DropDownList id="dropDownList"
dataProvider="{employees}"
labelField="LASTNAME"/>
</s:FormItem>
text property is being passed from the DropDownList control to dropDownList.selectedItem.PHONE:<s:FormItem label="Office Phone:">
<s:TextInput id="phone"
text="{dropDownList.selectedItem.PHONE}"/>
</s:FormItem>
In this section you will generate a fault event.
In this section you will use the Flash Builder Debugger to view the information returned from the fault event.
<s:WebService id="employeeService"
wsdl=http://www.adobetes.com/f45iaw100/remoteData/employeeData.cfc?wsd
result="employeeService_resultHandler(event)"
fault="employeeService_faultHandler(event)"/>
employeeService_faultHandler() function.employeeService_faultHandler() function (see Figure 12).
faultString property and value (see Figure 13).
In this section you will use the Alert class to create a pop-up dialog box to display fault data.
employeeService_faultHandler() function.show() method of the mx.controls.Alert class to display the event.fault.faultString value as text in an Alert pop-up. Make the title of the alert Fault Information.Within the Script block, note the import statement for the mx.controls.Alert class. If this import statement is not present, add it to your code to make the application work.
protected function employeeService_faultHandler(event:FaultEvent):void
{
Alert.show(event.fault.faultString,"Fault Information");
}
You will see the Alert dialog box display the fault event information (see Figure 14).
In this tutorial you learned how to use a WebService object to retrieve data from a dynamically generated WSDL document.
This work is licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 Unported License. Permissions beyond the scope of this license, pertaining to the examples of code included within this work are available at Adobe.