Recipe 21.2. Handling
Web Services Responses
Problem
You want to retrieve the return value from a web
services method.
Solution
Add a result event listener to the web
services method object.
Discussion
Web services methods are complex objects of type
mx.rpc.soap.Operation, to which you can assign event
listeners. When a web services method returns a value, the method
object dispatches a result event of type
mx.rpc.events.ResultEvent. If you want to handle the event,
you can add a listener to the method object. For example, if
webService is a WebService object that maps to a
web service that defines a method called getAverages( ), you
can add a listener as follows:
webService.getAverages.addEventListener(ResultEvent.RESULT, onGetAverages);
You then call the method normally:
webService.getAverages( );
When the listener is called, it is passed a
ResultEvent parameter.
The ResultEvent class defines a property called
result that contains the return value. Assuming
the getAverages( ) web services method returns an
associative array with two properties called flash and
actionscript, the following displays those values in a
text area called textArea:
private function onGetAverages(event:ResultEvent):void {
textArea.text = "The averages for Flash and ActionScript are " +
event.result.flash + " and " + event.result.actionscript;
}
|