Making Flex Web Service calls in ActionScript

I was using Flex the other day to consume some SOAP web services. However, I needed a way to modify the SOAP header in the request. This is exposed by calling the addHeader method on the service. That was easy enough, but I was finding my code difficult to manage when the service was defined using MXML and I had to add the header in ActionScript. I never really liked using MXML to define non-ui elements, so I decided to just use ActionScript to define the entire service. I found that it wasn’t as straight forward as I thought it would be so I thought I’d post my code here for the benefit of the community.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
import mx.rpc.events.FaultEvent;
import mx.rpc.events.ResultEvent;
import mx.rpc.soap.Operation;
import mx.rpc.soap.WebService;
 
var service:WebService = new WebService();
service.wsdl = "http://webservices.amazon.com/AWSECommerceService/AWSECommerceService.wsdl";
service.loadWSDL();
var operation:Operation = new Operation(null, "ItemSearch");
operation.addEventListener(ResultEvent.RESULT, onResult, false, 0, true);
operation.addEventListener(FaultEvent.FAULT, onFault, false, 0, true);
operation.request = <aws:ItemLookup
	xmlns:aws="http://webservices.amazon.com/AWSECommerceService/2007-10-29">
	<aws:AWSAccessKeyId>[YOUR_AWS_ID]</aws:AWSAccessKeyId>
	<aws:Request>
		<aws:Author>Danny Patterson</aws:Author>
		<aws:SearchIndex>Books</aws:SearchIndex>
	</aws:Request>
</aws:ItemLookup>;
service.operations = [operation];
operation.send();

An interesting note about the above code. I noticed the service wouldn’t work if I passed in the WebService instance into the Operation constructor. The operation must be added to the service by setting it in the service’s operations property.

5 thoughts on “Making Flex Web Service calls in ActionScript

  1. gunny

    When using FleXtense to auto generate the classes how can the errorhandling be done to display user friendly messages to the user.
    can u explain.
    thanks

    Reply
  2. ROhit Marathe

    I tried your code, which is in the class and I am calling callLogin method from mxml. But it does not give error and also never hit result or fault functions. Can u tell me the reason for this

    package components.Core
    {
    import mx.controls.Alert;
    import mx.rpc.events.FaultEvent;
    import mx.rpc.events.ResultEvent;
    import mx.rpc.soap.WebService
    import mx.rpc.soap.Operation;

    public class clsWebServices
    {
    public var loginService:WebService = new WebService();

    public function clsWebServices()
    {
    initiateWebServices();
    }
    private function initiateWebServices():void
    {
    loginService.destination = “http://localhost:1150/IDentistWebServices/Service.asmx?wsdl”;
    loginService.loadWSDL();
    }
    public function callLogin(userName:String, password:String, resultHandlerFunction:Function, faultHandlerFunction:Function):void
    {
    /* var loginRequest:Operation = new Operation(null,”getScanCenterData”);
    loginRequest.addEventListener(ResultEvent.RESULT, defaultResultHandler, false, 0, true);
    loginRequest.addEventListener(FaultEvent.FAULT , defaultFaultHandler, false, 0, true);
    loginService.operations = [loginRequest];
    loginRequest.send(); */
    var service:WebService = new WebService();
    service.wsdl = “http://webservices.amazon.com/AWSECommerceService/AWSECommerceService.wsdl”;
    service.loadWSDL();
    var operation:Operation = new Operation(null, “ItemSearch”);
    operation.addEventListener(ResultEvent.RESULT, defaultResultHandler, false, 0, true);
    operation.addEventListener(FaultEvent.FAULT, defaultFaultHandler, false, 0, true);
    operation.request =
    service.operations = [operation];
    operation.send();

    }

    private function defaultFaultHandler(event:Object):void
    {
    Alert.show(event.result.toString());
    }

    private function defaultResultHandler(event:ResultEvent):void
    {
    Alert.show(event.message.toString());
    }

    }
    }

    Reply
  3. Danny Patterson

    I suggest using something like Charles or Service Capture to monitor your network traffic and see if the service is even making the request or if its failing. This is a valuable debugging technique.

    Reply
  4. Ameya Suvarna

    Rohit,

    I used the Responder class to capture result from AsyncToken instead of adding an event listener to operation. It’s working for me now.

    CODE ADDED
    [
    var async:AsyncToken = operation.send();
    var responder:Responder = new Responder( defaultResultHandler, defaultFaultHandler);
    async.addResponder(responder);
    ]

    Now the event handler’s are getting triggered as soon as the Asynchronous call ends and I am able to retrieve the data.

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *