by
kirupa |
7 September 2010
Have questions? Discuss this Flash / ActionScript tutorial
with others on the forums.
In the
previous page, you received a brief intro to what web services are.
More importantly, you told Flash to look in the Flash Builder libraries
folder for any classes that may be referenced inside them. In this page,
we'll build your application and look at how communication with a web
service works.
With Flash now correctly setup to deal with your project, let's go ahead
and focus our eyes on the code. All of our code lives inside
MainDocument.as, so ensure that document is open and currently displayed.
As you
can see, some code is already provided for you inside
MainDocument.as:
- package
- {
- import
flash.display.MovieClip;
- import
flash.events.MouseEvent;
-
- public
class MainDocument
extends
MovieClip
- {
- public
function
MainDocument()
- {
- // setting up an event with an
event handler
- startButton.addEventListener(MouseEvent.CLICK,
SetupWebService);
- }
-
- function
SetupWebService(event:MouseEvent):void
- {
-
- }
- }
- }
Don't worry, the code that exists here is pretty boilerplate. These
few lines of code just say, "Call the SetupWebService method when you
click on the button."
Now that you know what you currently have on your plate called
MainDocument.as, let's go ahead and pile some more code on.
Replace all of your code with the code you see below:
- 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
- {
- var
url:String
=
"http://www.kirupafx.com/WebService/TopMovies.asmx?WSDL";
-
- movieWebService
=
new
WebService();
- movieWebService.loadWSDL(url);
-
- 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;
-
- }
-
- }
-
- }
The code that was there earlier is still there. I just figured
copying and pasting the entire code would be easier than having you
selectively paste the code that is different.
Once you have all of this code pasted, you should have everything
necessary for making your application connect to a web service and
return some data. Save this file and test your application by going to
Control | Test Movie | Test or by
pressing Ctrl + Enter.
Now that you have a working application that connects to a web service,
let's understand how it all works. Before we get to the code,
though, it is helpful to have an overview of what exactly happens when
your application communicates with a web service.
The following six steps summarize the life of your data
as it is communicated to your web service:
- In order to communicate with a web service, you need
to construct a request that defines what you want the
web service to do to you.
The first step in
that process is
to specify the URL of the web service you want to connect to. In our example,
the web service URL looks as
follows:
http://www.kirupafx.com/WebService/TopMovies.asmx
- Once you specify the web service URL, you need to figure out what
you want to ask the web service. The operations a web service supports
is generally documented by the web service itself and can be accessed by
pasting the web service URL into the browser:
[ the list of operations this web service contains ]
- After you figure out which operation you want to use, the next step
is to pass in the data that completes the operation.
Note
- This step is
optional, for some web service operations do not require any data
outside of simply calling the method associated with the operation.
- I can hear drumrolls! The next step is to actually make the web service request.
All of this time you were merely constructing the formal request. You
need to package it up and send it over to the web service.
- Once your request has been sent, the last step is to wait for a response and handle the response
appropriately.
The response may result in you getting the
data that
you want, or the response may give you some intermediate information that you will
need to make the web service request again. What you get back is
determined by the web service itself, and many web
services are quite unique in how they operate.
Ok, now that you have a brief overview how data is
communicated to and from a web service, let's look at how
these six steps are mapped in the code.
Onwards to the
next page!
|