2 May 2011
Beginning
In earlier exercises, you have used the HTTPService component to retrieve data from both static and dynamic XML sources. In this exercise, you will use an HTTPService object to send data to a server to insert it into the database. You will also handle the confirmation message from the server to display in an alert dialog box (see Figure 1).
Remember that, in a previous exercise, you consolidated the form data in a value object and dispatched it from a custom component using a custom event.
In this exercise, you will learn how to:
In this section you will create a HTTPService instance that will be used to send information to the server.
Declarations block, below the HTTPService instance, create another HTTPService instance by typing HTTP and pressing Ctrl+space twice to show code templates with content assist. Select the HTTPService object code template.<fx:Declarations>
<s:HTTPService id="employeeService"
url="http://www.adobetes.com/f45iaw100/remoteData/employees.xml"
result="employeeService_resultHandler(event)"
fault="employeeService_faultHandler(event)"
/>
<s:HTTPService id="service"
url="url"
fault="onFault(event)"
result="onResult(event)"
resultFormat="object"
showBusyCursor="true"/>
</fx:Declarations>
HTTPService object, modify the id property to have a value of vehicleService and the url property to have a value of http://www.adobetes.com/f4iaw100/remoteData/addVehicleRequest.cfm, and the method property with a value of POST.method property with a value of POST and delete the fault, result, resultFormat, and showBusyCursor properties.<s:HTTPService id="vehicleService"
url="http://www.adobetes.com/f4iaw100/remoteData/addVehicleRequest.cfm"
method="POST"/>
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.
In this section you will add the code to make the HTTPService request and transfer the data from the form, as a value object dispatched from a custom component using a custom event, to the server.
component:VehicleRequestForm instance below the UI components comment.vehicleRequestEvent property with a value of vehiclerequestform1_vehicleRequestEventHandler(event).The vehiclerequestform1_vehicleRequestEventHandler() function was created in Exercise 3.3: Dispatching a value object from the custom component to handle the vehicleRequestEvent custom event in the main application.
Script block, locate the vehiclerequestform1_vehicleRequestEventHandler() function.Alert.show() method.vehicleService.send() method to pass the event.vehicleRequestData value to the server.protected function vehiclerequestform1_vehicleRequestEventHandler(event:VehicleRequestEvent):void
{
vehicleService.send(event.vehicleRequestData);
}
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 Flash Builder Network Monitor to analyze the result of the vehicleService HTTPService object data transfer and response.
vehicleService HTTPService object, use the content assist tool to generate the result event (see Figure 2).
Script block, locate the vehicleService_resultHandler() function and delete the generated code stub..
Note the red circle with an X in it, signifying an error has occurred.
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 'ex3_04_http_starter.'"
You should see the Response data. Note that the first line tells you that you have experienced the 500 Error Executing Database Query.
Next, you will add a fault handler to show an Alert message when the Mobile Phone field is left blank.
vehicleService HTTPService object, add a fault property and, using the content assist tool, generate the fault handler.vehicleService_faultHandler() method in the Script block and delete the generated code stub.Alert.show() method to display the event.fault.faultString value as the text in the Alert pop-up.protected function vehicleService_faultHandler(event:FaultEvent):void
{
Alert.show(event.fault.faultString,"Fault Information for POST");
}
In this section, you will use the HTTPService object's result event and the id value to determine if the value object has been successfully written to the data table on the server.
if statement code template. You should see an if conditional statement added to your code.protected function vehicleService_resultHandler(event:ResultEvent):void
{
if()
{
}
}
if statement to evaluate the event.result.vehicleData.vehicleRequest.id value.protected function vehicleService_resultHandler(event:ResultEvent):void
{
if(event.result.vehicleData.vehicleRequest.addStatusid)
{
}
if statement, use the Alert.show() method to display the text: The request was submitted. The record id is and then concatenate the event.result.vehicleData.vehicleRequest.id value.protected function vehicleService_resultHandler(event:ResultEvent):void
{
if(event.result.vehicleData.vehicleRequest.addStatusid)
{
Alert.show("The request was submitted. The record id is " + event.result.vehicleData.vehicleRequest.id);
}
}
You should see the Alert window display the message that your request was submitted successfully and you should also see the record id (see Figure 9).
In this tutorial you learned use an HTTPService object to send data to a server to insert it into the database. You also learned how to handle the confirmation message from the server to display an alert dialog box.