In this exercise, you will use an HTTPService object with result and fault events to populate the sample application with data and handle an invalid data URL (see Figure 1).

Figure 1. Review your task for this exercise.
In this exercise, you will learn how to:
In order to complete this tutorial, you need the following software and files:
Exercise files:
In this section, you will use an HTTPService object to retrieve the data from the employees.xml file.
<!-- Declarations ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ --> <fx:Declarations> </fx:Declarations>
HTTPService object instance:
<fx:Declarations> <s:HTTPService/> </fx:Declarations>
HTTPService object, assign the id property value to employeeService and the url property value to
http://adobetes.com/f4iaw100/remoteData/employees.xml:
<fx:Declarations> <s:HTTPService id="employeeService" url="http://adobetes.com/f4rca100/remoteData/employees.xml"> </fx:Declarations>
Note: You can browse to the URL to see the XML data.
In this section you will retrieve the XML data using the HTTPService object and the creationComplete event of the main application.
To the opening Application tag, add the creationComplete event and assign its value to the employeeService.send() method:
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009" xmlns:s="library://ns.adobe.com/flex/spark" xmlns:mx="library://ns.adobe.com/flex/halo" minWidth="1024" minHeight="768" creationComplete="employeeService.send()">
In this section, you will map the result of the HTTPService object to Flex DataGrid and Label controls.
UIcomponents comment.DataGrid control instance:
<!-- UI components ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ --> <mx:DataGrid/>
Note: In this exercise, you are simply using the DataGrid control as a simple mechanism to display data. You will learn more about the DataGrid control in later videos.
DataGrid control, bind the dataProvider property value to the last result from the employee data returned by the employeeService HTTPService object, employeeService.lastResult.employees.employee.The DataGrid control is populated with data (see Figure 2).

Figure 2. Run the application to see the populated DataGrid control.
Note: You will not be able to scroll to the left to see all of the data. Remember from a previous video that scrollbars are not created by default in most Flex applications. The point for this exercise is to verify that data is returned to the DataGrid control. You will explore how to display this data more efficiently as this training series progresses.
DataGrid control, create a Label control instance.
<mx:DataGrid dataProvider="{employeeService.lastResult.employees.employee}"/>
<s:Label/>
employeeService.lastResult.employees.employee array collection.
Note: Remember that ActionScript is a zero-indexed language. In other words, arrays, including ArrayCollection objects, start counting at zero.
<s:Label text="{employeeService.lastResult.employees.employee.getItemAt(3).firstName}"/>
You should see the name Ben displayed below the DataGrid control (see Figure 3).

Figure 3. Display the fourth data item firstname value.
In the last section, you used the lastResult property of the returned data to bind the content to a DataGrid control. That is a very quick way to display data. However, if you want to manipulate the data first, or place it in a reusable variable, you should use the result event to handle the data and then bind it to the DataGrid control.
In this section, you define the result event handler for the HTTPService object.
Script comment.Script comment, create a Script block:
<!-- Script ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ --> <fx:Script> <![CDATA[ ]]> </fx:Script>
HTTPService object.Within the HTTPService object tag, use the code lookup tool to generate the result event (see Figure 4).

Figure 4. Use the code lookup tool to generate the result event for the HTTPService object.
Using the code lookup tool to generate the result event causes the code lookup tool to display the Generate Result Handler option (see Figure 5).
Click the menu option or press Enter to generate the result handler function (see Figure 5).

Figure 5. Use the code lookup tool to generate a result handler function for the HTTPService object.
Script block.import statement and employeeService_resultHandler() function:
<!-- Script ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
<fx:Script>
<![CDATA[
import mx.rpc.events.ResultEvent;
protected function employeeService_resultHandler(event:ResultEvent):void
{
// TODO Auto-generated method stub
}
]]>
</fx:Script>
In this section, you will use the Flash Builder debugger to examine the data returned by the HTTPService object.
Double-click the Editor gutter on the same line as the closing brace of the employeeService_resultHandler() function to add a breakpoint (see Figure 6).

Figure 6. Place a breakpoint on the ending line of the employeeService_resultHandler() function.
Click the Debug button to debug the application (see Figure 7).

Figure 7. Click the Debug button.
When you see the Confirm Perspective Switch dialog box (see Figure 8), click Yes to switch to the Flash Builder Debug perspective.

Figure 8. Click Yes to switch to the Flash Builder Debug perspective.
In the Variables view, expand the event object to reveal the result event and the employee data being pulled into the application (see Figure 9).

Figure 9. Expand the result object to reveal the employees data.
Click the Terminate button to end the debugging session (see Figure 10).

Figure 10. Terminate the debugging session.
Remember that the event object inside the employeeService_resultHandler() function is only available within that function. To access that data outside the function, you will place the returned data into a class property.
In this section, you will place the returned data into a variable that is data typed to the ArrayCollection class.
Script block.Script block, below the import statement, import the mx.collections.ArrayCollection class.
<fx:Script>
<![CDATA[
import mx.rpc.events.ResultEvent;
import mx.collections.ArrayCollection;
protected function employeeService_resultHandler(event:ResultEvent):void
{
// TODO Auto-generated method stub
}
]]>
</fx:Script>
import statement, declare a bindable private variable named employees and data type it to the ArrayCollection class:
... import mx.collections.ArrayCollection; [Bindable] private var employees:ArrayCollection; ...
employeeService_resultHandler() function.employees variable (the new instance of the ArrayCollection class):
protected function employeeService_resultHandler(event:ResultEvent):void
{
employees = event.result.employees.employee;
}
components comment, locate the DataGrid control.DataGrid control's dataProvider property value so that it is bound to the employees class variable.
<mx:DataGrid dataProvider="{employees}"/>
Label control's text property value so that it uses the employees class property to display the fourth employee's firstName value in the data.
<s:Label text="{employees.getItemAt(3).firstName}"/>
You should see there has been no visual change to the application; the DataGrid control and Label control display the same information as they did before (see Figure 11).

Figure 11. Run the application to see no change
In this section, you will use the fault event to handle an invalid URL error.
HTTPService object.For the HTTPService object, use the code lookup tool to generate the fault event (see Figure 12).

Figure 12. Use the code lookup tool to generate the fault event for the HTTPService object.
Using the code lookup tool to generate the fault event causes the code lookup tool to display the Generate Result Handler option (see Figure 13). Click the menu option or press Enter to generate the fault handler function.

Figure 13. Generate a fault handler function.
In this section, you will use the Flash Builder debugger to view the information returned from the fault event.
HTTPService object url property value, remove the last character so that the URL will be invalid:
<fx:Declarations> <s:HTTPService id="employeeService" url="http://adobetes.com/f4iaw100/remoteData/employees.xml" result="employeeService_resultHandler(event)" fault="employeeService_faultHandler(event)"/> </fx:Declarations>
employeeService_faultHandler() function:
protected function employeeService_faultHandler(event:FaultEvent):void
{
// TODO Auto-generated method stub
}
Move the breakpoint to the same line as the closing brace of the employeeService_faultHandler() function (see Figure 14).

Figure 14. Place the breakpoint on the same line as the ending brace of the employeeService_faultHandler() function.
In the Variables view, expand the event object to reveal the fault event (see Figure 15).
Specifically take note of the faultString property and value.

Figure 15. View the debugging information for the fault event in the Variables view.
In this section, you will use the Alert pop-up dialog box to display fault data.
Within the Script block, locate the employeeService_faultHandler() function:
protected function employeeService_faultHandler(event:FaultEvent):void
{
// TODO Auto-generated method stub
}
mx.controls.Alert class' show() method to display the event.fault.faultString value as text in an Alert pop-up. Use the string Fault Information for the title of the alert:
protected function employeeService_faultHandler(event:FaultEvent):void
{
mx.controls.Alert.show(event.fault.faultString,"Fault Information");
}
You will see the Alert dialog box displaying the fault event information (see Figure 16).

Figure 16. View the fault information within an Alert class dialog box.
In this exercise, you learned how to use result and fault events with the HTTPService object.
result event.faultString property.send() method.Trilemetry, Inc is a development and education organization that implements a human-centered design approach to the creation of software and content. Their Adobe portfolio includes the Adobe ColdFusion Getting Started Experience, the Adobe Flex Getting Started Experience, the Flex in a Week video series, the official Adobe instructor-led training course Flex 3: Extending and Styling Components and more. They also create and support many Web applications from interactive Flash sites and corporate web sites to mission-critical business applications.