2 May 2011
Beginning
The HTTPService component can retrieve data over the Hypertext Transfer Protocol (HTTP). Since the returned data is not typed, this method is largely used to return simple strings or XML data, which can be static or dynamically generated.
In this exercise you will use the HTTPService object to retrieve dynamically generated content from a remote application server by supplying a direct URL to the template. You will retrieve all of the fictional employees in a company to display in a DropDownList control (see Figure 1). Lastly, you will learn how to use the fault event to show an alert message in the event of retrieval error.
In this exercise you will learn how to:
In this section, you will create an instance of an HTTPService component to retrieve dynamically generated XML from a remote server.
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 dynamically generated XML shown in Figure 2.
Declarations block.http://adobetes.com/f45iaw100/remoteData/employeeData.cfm.<s:HTTPService id="employeeService"
url="http://adobetes.com/f45iaw100/remoteData/employeeData.cfm"
result="employeeService_resultHandler(event)"/>
Everything runs exactly the same as the previous exercise. The only difference is that you are now pulling XML data that is dynamically generated, rather than statically built.
Declarations block, locate the HTTPService object.HTTPService object, use the content assist tool to generate the fault event (see Figure 3).
Using the content assist tool to generate the fault event causes the Generate Fault Handler option to display (see Figure 4).
In this section, you will use the Flash Builder Debugger to view the information returned from the fault event.
url property value, remove the last character so that the URL will be invalid:<fx:Declarations>
<s:HTTPService id="employeeService"
url="http://adobetes.com/f45iaw100/remoteData/employeeData.cf"
result="employeeService_resultHandler(event)"
fault="employeeService_faultHandler(event)"/>
</fx:Declarations>
Script block, locate the generated employeeService_faultHandler() function.protected function employeeService_faultHandler(event:FaultEvent):void
{
// TODO Auto-generated method stub
}
employeeService_faultHandler() function (see Figure 5).
fault event (see Figure 6).Note the faultString property and value.
In this section, you will use the Flex Alert class to create a pop-up dialog box to display fault data.
Script block, locate the 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 is not present, you will have to add it 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 8).
In this exercise you learned how to use an HTTPService object to retrieve data from an dynamically generated XML file and use the fault event.
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.