Using XML in Flex - Tutorial Part 3 - HTTPService
This part of our tutorial on using XML in Flex 2.0 applications will focus on the HTTPService ActionScript 3.0 class and the mx:HTTPService MXML tag. My references for this tutorial are the Adobe Flex 2 Language Reference, Programming ActionScript 3.0, and the Flex 2 Developer's Guide.
Information about the HTTPService ActionScript class and MXML tag is in the Using RPC Components section of the Flex 2 Developer's Guide. RPC stands for Remote Procedure Call.
Let's examine class HTTPService first. Here is a direct link to the class description in the Flex 2 Language Reference. Class HTTPService is used to make a get or post request to another URL. The URL used displays some data that is captured and returned to the Flex application. Often this data is an XML file. Class HTTPService has a URL property that you set to the URL you want to get data from.
The send method is used to tell the HTTPService object to connect to the URL. An optional parameter for the send method is an object that contains name-value pairs. Sometimes the URL you are connecting to requires data to perform its tasks so you create a generic Object and add name and their values.
As an example of using class HTTPService, please view this Flex 2.0 Application. You can right-click on the application to see the source code.
Flex has an MXML tag named mx:HTTPService that can also be used to connect to a URL and get the data the provided by that URL. Here is a direct link to the MXML tag description in the Flex 2 Language Reference.
As with the HTTPService ActionScript class, you use the URL property to identify the resource to connect to and the send method to make the connection.
You can embed an mx:Request tag inside the mx:HTTPService to specify the values for any prameters the web service requires. The value for the mx:Request tag can be bound to a Flex control such as a text input or combo box. Nested inside the mx:Request tag will be tags that have the name of the parameter and these tags will contain the values. For example:
<mx:request>
<deptId>{dept.selectedItem.data}</deptId>
</mx:request>
Another way to pass data to the URL web service is to create an Object that includes the name-value pairs and use this Object as the argument for the send method as we did with class HTTPService.
When the data is returned to the Flex application, a lastResult object is available. If you set the resultFormat property of the mx:HTTPService tag to "e4x", the the lastResult object is recognized as XML. You can then use the notation I described in the first XML tutorial to access a specific node's value. You can bind the lastResult object or a node value to other Flex controls.
You can view the same type of example as above, only using the HTTPService MXML component instead of ActionScript 3.0 class HTTPService. You can right-click to view the source.
There are no comments for this entry.
[Add Comment]