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

4 Responses to “Learning Flex – Lesson 17, Working with Web Services”

  1. Craig Kaminsky Says:

    Very nice post! I wanted to comment because I ran into some serious issues dealing with SOAP XML namespaces with complex data/results returned from a .NET web service.

    I ended up having to put together an AS class to strip the namespaces from the returned SOAP so I could better work with the results (ArrayCollections, etc.).

    I wrote a post about it and have the code sample on the same post (below). I swear this isn’t a plug for my blog :)!

    It was a pain in the butt to get around the namespace issue and none of the examples I read when learning about Flex and Web Services (especially .NET web services) dealt with complex data types in your SOAP response. So, I hope to offer this as help for anyone who runs into issues dealing with parsing SOAP XML on the return.

    http://craigkaminsky.blogspot.com/2009/05/flex-utility-class-for-removing.html

    • mattreyuk Says:

      Hi Craig, thanks. There’s a section at the end of the Adobe livedocs I linked to at the end of the post ( http://livedocs.adobe.com/flex/3/html/help.html?content=data_access_3.html ) about customizing web service type mapping that seems to suggest you should be able to map your web service response directly to an ActionScript class did you ever try that?

      Looks like you’ve got some other useful practical posts on using web services with Flex so I’ll be checking them out too.

  2. Ernest Says:

    Hi, is it possible to declare a fault handler for any kind of request for all my webservices in the application without having to manually change the fault operation in the mxml?
    Thanks

    • mattreyuk Says:

      Hi Ernest – not sure what you’re asking here exactly – yes you can declare a global fault handler either by providing your fault operation name to the fault property in the WebService tag or in Actionscript by using the addEventListener() method of the WebService class. Is that what you’re looking for?


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: