Sunday, May 20, 2007

mx.rpc.soap.WebService.ActionScriptExample

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" applicationComplete="creationApplicationListener(event)">
   <mx:Script>
      <![CDATA[
         import mx.events.FlexEvent;
         import mx.rpc.events.FaultEvent;
         import mx.rpc.events.ResultEvent;
         import mx.rpc.soap.LoadEvent;
         import mx.rpc.soap.WebService;

         public var _webService:WebService; // web service
         public var _webServiceLoaded:Boolean = false; // flag noting when wsdl has been loaded

         public function initWebService():void {
            // initializes webservice
            if (!_webService) {
               _webService = new WebService();

               // assign wsdl url
               _webService.wsdl = "http://www.domain.com/HelloWorld/Service.asmx?WSDL"; // replace this url

               // add listeners
               _webService.addEventListener(LoadEvent.LOAD, load_listener);
               _webService.addEventListener(ResultEvent.RESULT, result_listener);
               _webService.addEventListener(FaultEvent.FAULT, fault_listener);

               // load wsdl
               _webService.loadWSDL();
            }
         }

         public function callHelloWorld():void {
            initWebService(); // init webservice
            if (_webServiceLoaded) { // is wsdl is loaded
               _webService.HelloWorld(); // call web service method called "HelloWorld" with no parameters
            } else { // else
               callLater(callHelloWorld); // wait a frame and try again
            }
         }

         public function load_listener(event:LoadEvent):void {
            _webServiceLoaded = true; // mark wsdl as loaded so we know we can use the web service
         }

         public function result_listener(event:ResultEvent):void {
            trace(event.result); // traces result of webservice method which in this example is "Hello World";
         }

         public function fault_listener(event:FaultEvent):void {
            trace(event.fault); // traces information about failure
         }

         public function creationApplicationListener(event:FlexEvent):void {
            // once application is loaded, call web service
            callHelloWorld();
         }

      ]]>
   </mx:Script>
</mx:Application>

5 comments:

Joel Lawler said...

Great stuff.

Thanks for posting this.

Anonymous said...
This comment has been removed by a blog administrator.
Anonymous said...
This comment has been removed by a blog administrator.
Anonymous said...
This comment has been removed by a blog administrator.
kristinahojholt said...

Hey! This is my first comment here so I just wanted to give a quick shout out and say I really enjoy reading through your articles. Can you recommend any other blogs/websites/forums that deal with the same topics? Thank you! epicor baq

"Flex", "ActionScript" and possibly "MXML" are probably trademarks of Adobe Systems Incorporated.
"Adobe" is a trademark of Adobe Systems Incorporated.
This site is in no way endorsed or sponsored by Adobe Systems Incorporated.
Content Copyright © 2007 Daniel Freiman.
Site Design Copyright by its copyright holder.
The Flex Non-Docs reserves the right to remove comments for any reason.
All ActionScript and MXML code (and ONLY ActionScript and MXML code) on this website is available under the MIT License.