Calling a WebService using Flash/AS3

Hi everyone,
It seems like the WebService classes have been removed for AS3 projects created in Flash. Fortunately, the Flash Builder libraries work in Flash projects, so I’ve created a small example involving a web service that uses the Flash Builder SWC files in a Flash CS4 project.

In order to build the example, make sure you have Flash Builder 4 installed. Also make sure your Library path contains a pointer to the Flash Builder libraries. On my computer it is: C:\Program Files (x86)\Adobe\Adobe Flash Builder 4\sdks\4.1.0\frameworks\libs You can set/modify that in the ActionScript 3.0 Settings dialog.

The code is basically:


package 
{
	import flash.display.MovieClip;
	import flash.events.MouseEvent;
	import mx.rpc.soap.*;
	import mx.rpc.events.*;
	import mx.rpc.AbstractOperation;

	public class MainDocument extends MovieClip
	{
		private var movieWebService:WebService;
		private var serviceOperation:AbstractOperation;

		public function MainDocument()
		{
			// setting up an event with an event handler
			startButton.addEventListener(MouseEvent.CLICK, SetupWebService);
		}

		function SetupWebService(event:MouseEvent):void
		{
			movieWebService = new WebService();
			movieWebService.loadWSDL("http://www.kirupafx.com/WebService/TopMovies.asmx?WSDL");

			movieWebService.addEventListener(LoadEvent.LOAD, BuildServiceRequest);
		}

		function BuildServiceRequest(evt:LoadEvent)
		{
			serviceOperation = movieWebService.getOperation("GetMovieAtNumber");
						
			serviceOperation.addEventListener(FaultEvent.FAULT, DisplayError);
			serviceOperation.addEventListener(ResultEvent.RESULT, DisplayResult);
			
			serviceOperation.send([GenerateRandomNumber(0,9)]);
		}

		function DisplayError(evt:FaultEvent)
		{
			trace("error");
		}
		
		function DisplayResult(evt:ResultEvent)
		{
			var movieName:String = evt.result as String;
			movieText.text = movieName;
		}
		
		function GenerateRandomNumber(min:int, max:int):int
		{
			return Math.floor(Math.random()*(1+max-min))+min;
		}
	}
}

I’ll have a tutorial written on this soon, but I wanted to post this up here in the interim.

Cheers,
Kirupa :stuck_out_tongue_winking_eye: