Learning Flex – Lesson 17, Working with Web Services

Flex applications can use web services that are defined by a WSDL (web Service Description Language) an XML based standard that defines available operations and possibly data types used by the operations.

Flex supports web service requests and responses that are formatted as SOAP (Simple Object Access Protocol) documents which are XML based.

There are two main ways to access web services in Flex, using tags or ActionScript.

Using the <mx:WebService> tag, the wsdl property specifies the URL to find the wsdl file at. Flex will load this file to find out the available operations. Once the wsdl file is loaded successfully, the WebService broadcasts a load event. You may also define a fault and result event handler for all interactions with the server.

The WebService tag may contain multiple <mx:operation> tags which have a name property for the service to be used and may specify result and fault event handlers specific to that operation.

Within the operation tag, you may specify an <mx:request> tag which can be used to specify the values for parameters to the operation (using binding for these values allows them to automatically update).

A fully declared WebService would look like this:

<mx:WebService
  id="myWebService"
  wsdl="http://myServer.com/wsdls/Services.wsdl"
  fault="handleError(event)">
    <mx:operation name="getAccountDetails" result="acountResult(event)">
      <mx:request>
        <name>{username}</name>
        <id>{userID}</id>
      <mx:request>
    </mx:operation>
</mx:WebService>

You can just specify the operation (declared method) within the WebService so you get to use the individual result/fault handlers and specify the parameters when you use the service or not bother with the operations at all (undeclared method) and specify everything when you use the service (but you’ll need to at least declare a result handler for the WebService in this case).

To call a WebService operation, you call the send() method on that operation, so for our previous definition, that would be:

myWebService.getAccountDetails.send();

To define a WebService using ActionScript, you must manually load the wsdl using the loadWSDL() method. Otherwise, it maps pretty closely as can be seen here:

private var myWS:WebService;
myWS = new WebService();
myWS.wsdl = "http://myServer.com/wsdls/Services.wsdl";
myWS.addEventListener("fault",handleError);
myWS.getAccountDetails.addEventListener("result",accountResult);
myWS.loadWSDL();

account = myWS.getAccountDetails(username,userID);

Note that you don’t need the send() this way.

You will run into naming issues if the operation you try to use happens to match a defined method of WebService. To get around this, you can use the getOperation() method on your WebService providing the operation name as a String. This will return an Operation for you to call using it’s send() method eg

myWS.getOperation("disconnect").send(param);

FlexBuilder has the capability to generate supporting code to use a wsdl’s operations by using the “import web service” wizard in the data menu.

You can find more detailed information on dealing with soap headers etc here. A useful tool when working with web services (to inspect wsdls, generate soap requests etc) is the free SoapUI tool.

Advertisements