Search Flex Components Free

Custom Search

December 9, 2007

Using the Flash MX 2004 Web Service Classes

Flash MX 2004 comes with two client side methods for connnecting to web services. The first is by using the web service connector component, the second is by using the web service classes. This tutorial deals with the web service classes, which can be found in the mx.services package. These classes can only be used with ActionScript and are not visual like the connector component. We'll be covering the WebService, PendingCall, Log, and SOAPCall classes in this tutorial.

The main advantage of using the web services class over the web service connector component is that you have a bit more control over the interaction between flash and the service, and the debugging is also more in-depth. The downside is that it can be more time consuming and binding the results to other components is not as easy as with the connector component. We'll be covering the connector component in the next tutorial in this series.

Both of the two methods for connecting to web services in Flash MX04 have a couple of disadvantages. The main being that it can be really slow when returning larger datasets. The reason for this is that, even with the mx2004 speed improvments, it can take a lot of processor power to convert the returned xml into flash actionscript objects. There are a couple of ways to mask this slow performance, such as turning on doLazyDecoding, but overall when returning large result sets from a web service it is a far better solution to go with Flash Remoting. Flash remoting will return the result set as AMF instead of XML which has a much faster transfer speed (much more compressed) and does not need to be converted to flash actionscript (as it already is).

The second disadvantage of using the new built in client side web service features of flash mx 2004 are the security features. In order to use a web service that is not located on the same server, the owner of that service would need to place a cross domain policy file allowing the flash movie access to it's service. This in a way defeats the purpose of web services, as shown by the following quote.

A simple way for any programming language from any server, device, other application, etc - to communicate with any other server, device, application in any programming language. - Intro to webservices tutorial.

To illustrate this point - what would be the chance of Amazon or Google implementing a cross domain policy file so that www.someSite.com could use flash to access there web services... () One thing to note: This security feature is only required when the movie is published and placed on a server, locally or in the flash authoring environment there are no restrictions. Using Flash Remoting with web services will eliminate this issue.

Lets get started
This tutorial covers one sample actionscript file. To start we go over the basic code needed, then add more code for different features as we move on. The tutorial will not be going over what to do with the returned data in flash, only how to get it there. To get started:

1: Open up flash mx 2004 and create a new flash document.
2: Drag a button component onto the stage and give it an instance name of result_btn.
3: ***Most Important***: You must include the WebService Classes, these are contained within a SWC file and must be present in your library in order to use the web services classes api. To include them:
Select the Classes library by selecting Windows > Other Panels > Common Libraries > Classes. Then drag an instance of the WebServicesClasses onto the stage.
And that's it for setup, the most important part is to drag an instance of the WebServicesClasses onto the stage (or into the documents library).

We are going to be testing with the Flash-db Company information service (that is also referenced in the ActionScript Dictionary). You can find out more details on this service here.

Another good way to find out information on any service is by using the Web Services Panel, which can be found at: Window > Development Panels > Web Services. To become familar with the web service that we will be using. Open up this panel and add in the following WSDL file: http://www.flash-db.com/services/ws/companyInfo.wsdl. It should look something like the following.

Related Flex Tutorials