22 March 2010
A basic understanding of creating applications in Flash Builder 4 and Flex is recommended.
Beginning
The Flex SDK includes three Remote Procedure Call (RPC) components that allow you to integrate your Flex applications with common application server products. Of these, the HTTPService component has the most flexibility in terms of the format of messages that are exchanged between the client and server at runtime. Unlike the other RPC components (WebService and RemoteObject), you can use the HTTPService component with any application server because it exchanges data in the form of simple HTTP parameters and XML of any flavor.
Flash Builder 4 adds a new set of code generation features. The data-centric development tools enable you to quickly build data-centric applications that trade data at runtime with multiple application servers using any of the RPC architectures.
In this tutorial, I’ll describe how to use the new Flash Builder 4 features to define a data connection—a set of ActionScript classes that enable requests to and retrieval of data from an application server.
Flash Builder 4 adds a new set of data-centric development features that make it easy to generate code for the following application tasks:
These features are available for use with the following servers and remote applications or data sources:
To get started, download the HTTPServiceTutorial.zip sample file and extract it anywhere on your hard disk. The ZIP file contains a Flash Builder 4 project in the new FXP project archive format named HTTPServiceTutorial.fxp.
Import the project into Flash Builder:
The contacts.xml file contains a set of fictitious contact information in XML format. The XML file’s root element is named <contacts>, and it contains repeating data elements named <row>:
<?xml version="1.0"?>
<contacts>
<row>
<contactid>1</contactid>
<firstname>Brad</firstname>
<lastname>Lang</lastname>
<streetaddress>3004 Buckhannan Avenue</streetaddress>
<city>Syracuse</city>
<state>NY</state>
<email>Brad.C.Lang@trashymail.com</email>
<phone>315-449-9420</phone>
</row>
<row>
<contactid>2</contactid>
<firstname>Kevin</firstname>
<lastname>Mount</lastname>
<streetaddress>341 Private Lane</streetaddress>
<city>Montgomery</city>
<state>GA</state>
<email>Kevin.J.Mount@trashymail.com</email>
<phone>229-329-4001</phone>
</row>
... more data ...
</contacts>
You can define a remote HTTP service in any Flex project. (When working with certain specific application servers such as ColdFusion, you must first associate the Flex project with that server.)
You’ll need the following information before you get started:
For this tutorial, you’ll use a static XML file that’s stored within the deployed application’s directory structure.
<s:layout>
<s:VerticalLayout horizontalAlign="center" paddingTop="20"/>
</s:layout>
Note: The dialog box displays a warning that the contacts.xml file should be relative to the output folder. When you build the project, the application’s non-embedded assets, including the contacts.xml file, are copied to the output folder, so the data will be available to the application at runtime.
It takes a few seconds to create everything you need to request the XML file from the server and parse it. The resulting code is created in the service package, which defaults to services.[service name]. Two ActionScript class are generated there; for example, the preceding steps create a new class named ContactService. This class is derived from a superclass, in this case named _Super_ContactService.
You can customize the subclass, which you’ll call directly in your application, adding custom properties and methods as needed. If you change the properties of the data connection in Flash Builder 4, the super class will be regenerated, but the subclass will be left intact with changes you might have made.
Data connections are managed in the new Flash Builder 4 Data/Services view. From this view, you can select a service and modify its properties, configure the return data types of any service class operations, and start the process of binding an operation’s data to a visual control.
Follow these steps to modify a service’s properties:
The connection is represented in the Flash Builder 4 interface in the Data/Services view, which in turn points to the set of classes previously described. Follow these steps to explore the generated code:
The code for this class is very simple; it extends another class named _Super_ContactService:
/**
* This is a generated sub-class of _ContactService.as
* and is intended for behavior customization. This class
* is only generated when there is no file already present
* at its target location. Thus custom behavior that you
* add here will survive regeneration of the super-class.
**/
package services.contactservice
{
public class ContactService extends _Super_ContactService
{
}
}
Note: The code in the generated class might not be as clean as the preceding example. I’ve added indentation and eliminated some of the white space in the generated code to make it more readable. But the comments tell you what you need to know: this class can be customized, and won’t be overwritten if you generate the data connection again.
The generated superclass code in _Super_ContactService.as, cleaned up for readability, looks like this:
/**
* This is a generated class and is not intended for modification. To
* customize behavior of this service wrapper you may modify the generated
* sub-class of this class - ContactService.as.
*/
package services.contactservice
{
import com.adobe.fiber.core.model_internal;
import com.adobe.fiber.services.wrapper.HTTPServiceWrapper;
import mx.rpc.AbstractOperation;
import mx.rpc.AsyncToken;
import mx.rpc.http.HTTPMultiService;
import mx.rpc.http.Operation;
[ExcludeClass]
internal class _Super_ContactService extends
com.adobe.fiber.services.wrapper.HTTPServiceWrapper
{
// Constructor
public function _Super_ContactService()
{
// initialize service control
_serviceControl = new mx.rpc.http.HTTPMultiService();
var operations:Array = new Array();
var operation:mx.rpc.http.Operation;
var argsArray:Array;
operation = new mx.rpc.http.Operation(null, "getContacts");
operation.url = "data/contacts.xml";
operation.method = "GET";
operation.resultType = Object;
operations.push(operation);
_serviceControl.operationList = operations;
model_internal::initialize();
}
/**
* This method is a generated wrapper used to call the 'getContacts' operation.
* It returns an mx.rpc.AsyncToken whose result property will be populated with the
* result of the operation when the server response is received.
* To use this result from MXML code, define a CallResponder component and assign
* its token property to this method's return value.
* You can then bind to CallResponder.lastResult or listen for the
*
CallResponder.result or fault events.
* @see mx.rpc.AsyncToken
* @see mx.rpc.CallResponder
*
* @return an mx.rpc.AsyncToken whose result property will be populated with the
* result of the operation when the server response is received.
*/
public function getContacts() : mx.rpc.AsyncToken
{
var _internal_operation:mx.rpc.AbstractOperation =
_serviceControl.getOperation("getContacts");
var _internal_token:mx.rpc.AsyncToken = _internal_operation.send() ;
return _internal_token;
}
}
}
After working through the remainder of this tutorial, you might want to return to this page and review the generated code. You’ll see that it mimics what you can do manually with the HTTPService component: request and parse data in XML format.
Note: Notice that the generated code uses a class named HTTPMultiService to send requests to the server. HTTPMultiService allows you to manage multiple related server locations, but shares most of the features of the HTTPService component.
When data is returned from the HTTPService component, it’s defined by default as an ActionScript Object. Flex applications can realize enormous benefits from using strongly typed value object classes instead of generic instances of Object. Flash Builder 4 can parse your returned XML data and automatically generate the required value object class. When the data is then returned from the server, it will appear in the application as the class you’ve selected.
Flash Builder 4 can dynamically generate value object code based on the XML data that will be received from the server. If you’re working with an application server, you can provide the page location’s URL and Flash Builder 4 will retrieve and parse the returned XML. If you’re working with a static XML file that you’ve included in your Flex project, you’ll need to open the XML file in Flash Builder 4 or another text editor and copy its contents to the system clipboard.
getContacts() operation and choose Configure Return Type from the context menu (see Figure 5).
In the first screen of the Configure Return Type wizard, you can select either auto-detection of the XML data structure, or select an existing ActionScript class to use as a value object.
Note: You typically assign the return type a name that is singular, as in Contact, rather than plural as in Contacts. You’re defining a value object ActionScript class that will represent a single instance of the data object.
The option Is array? is selected automatically.
contactid property’s Type to int.In the Data/Services view, the getContacts() operation now shows a return type of Contact[] (see Figure 9). The [] characters indicate that the returned data will be in the form of an Array of instances of the Contact class.
You’ll find three new ActionScript classes in the Flex project’s valueObjects folder:
The Contact.as file contains the subclass declaration and a call to a static initializer method:
package valueObjects
{
import com.adobe.fiber.core.model_internal;
public class Contact extends _Super_Contact
{
public static function _initRemoteClassAlias() : void
{
_Super_Contact.model_internal::initRemoteClassAliasSingle(valueObjects.Contact);
_Super_Contact.model_internal::initRemoteClassAliasAllRelated();
}
model_internal static function initRemoteClassAliasSingleChild() : void
{
_Super_Contact.model_internal::initRemoteClassAliasSingle(valueObjects.Contact);
}
{
_Super_Contact.model_internal::initRemoteClassAliasSingle(valueObjects.Contact);
}
}
}
The superclass (_Super_Contact.as) defines the class’s private properties and implements matching getter/setter accessor methods for each:
[ExcludeClass]
public class _Super_Contact extends EventDispatcher implements IValueObject
{
model_internal var _internal_model : _ContactEntityMetadata;
private var _internal_contactid : int;
private var _internal_firstname : String;
private var _internal_lastname : String;
... more private properties ...
[Bindable(event="propertyChange")]
public function get contactid() : int
{
return _internal_contactid;
}
[Bindable(event="propertyChange")]
public function get firstname() : String
{
return _internal_firstname;
}
... more setters and getters ...
}
If you’re interested, you can open these generated classes on your system and explore them in depth; or you can move on to the next step, binding the returned data to a visual control.
After defining an HTTP connection and its operations, and then configuring an operation’s return type, the next step is to display the returned data in a visual control.
Note: You can also start the binding operation by choosing Data > Bind to Data.
getContacts():Contact[].
You should see that the DataGrid displays all of the data properties returned from the operation as column headers.
When the application starts, you’ll see that it retrieves and displays the data (see Figure 13).
In some conditions, you might see a runtime security error as shown in Figure 14.
This error occurs because you’re loading an XML file from the local hard disk, rather than over the network. If you see this error, follow these steps to allow the application to load the XML file locally:
-locale setting:-use-network=false
Flash Builder 4 also can generate a form based on the structure of the data returned from the XML file. In these steps, you’ll create a read-only form that displays detailed data selected from the DataGrid control.
The Generate Form dialog box shows that the form will show the data returned from the getContacts() operation (see Figure 16).
The form will appear just below the DataGrid control (see Figure 17).
Next, you’ll add a line of ActionScript code that causes the form to display the row of data that the user selects in the DataGrid.
Flash Builder 4 switches to Source mode and generates a new ActionScript function named dataGrid_changeHandler().
contact = event.target.selectedItem as Contact
The complete function should look like this:
protected function dataGrid_changeHandler(event:ListEvent):void
{
contact = event.target.selectedItem as Contact;
}
You can learn more the HTTPService component and other RPC tools in the Flex SDK from the Flex 4 documentation:
Also, be sure to visit the Flex Developer Center for more helpful tips and tutorials.