Recipe 21.1. Calling Web Services Methods


Problem

You want to call a web service method.

Solution

Use an mx.rpc.soap.WebService object, and call the method from the WebService object.

Discussion

As mentioned earlier, Flash Player has no built-in web services capabilities, but the Flex framework does include a solution. This recipe discusses how to use the Flex 2 solution for working with web services. For a Flash solution, see additional notes at http://www.rightactionscript.com/ascb.

The Flex framework includes mx.rpc.soap.WebService, a class that simplifies calling web services methods by using ActionScript. The first step is to construct a new WebService object, as follows:

var webService.WebService = new WebService(  );

Every web service must have a Web Service Description Language (WSDL) resource that describes the service. You can tell the WebService object where to locate the WSDL using the wsdl property:

webService.wsdl = "http://www.rightactionscript.com/webservices/FlashSurvey.php?wsdl";

Before you can call methods, you must load the WSDL data from the specified URL by using the loadWSDL( ) method:

webService.loadWSDL(  );

The loadWSDL( ) method makes a request for the WSDL data asynchronously. That means you have to listen for an event to know when the WSDL data has loaded. When the data has loaded, the WebService object dispatches a load event of type mx.rpc.soap.LoadEvent, as shown here:

webService.addEventListener(LoadEvent.LOAD, onWSDL);

Once the WSDL data has correctly loaded, call the web services methods directly from the WebService object. For example, the WSDL URL used in this example points to a real web service, which has a method called getAverages( ) that can be called from the WebService object, as follows:

webService.getAverages(  );

If the method happens to expect parameters, pass them to the method as you would any normal ActionScript method. For example, the same web service also has a method called takeSurvey( ), which expects two integer values; the following calls the method and passes it values of 10 and 15:

webService.takeSurvey(10, 15);

Web services method calls occur asynchronously, which means a response is not returned immediately. See Recipe 21.2 for more details on how to retrieve the return value from a web services method.

See Also

Recipe 21.2




ActionScript 3. 0 Cookbook
ActionScript 3.0 Cookbook: Solutions for Flash Platform and Flex Application Developers
ISBN: 0596526954
EAN: 2147483647
Year: 2007
Pages: 351

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net