Tutorial – Consuming REST web services in ActionScript 3 – Part 2

8 actionscript,as3,as3 tutorials,flash,webservices,work  |   March 14, 2008  |     4612

Hi Everyone,

the first part of this tutorial can be found here: Tutorial – Consuming REST web services in ActionScript 3 – Part 1.

Today I will continue on the exploration of consuming REST web services in ActionScript 3. We will cover possible errors first, then create a versatile web service client object which is easily adaptable to all your projects.


Yesterday in my exploration of the new YouTube Data API roll-out, I ran into a wall concerning PUT and DELETE requests. The method described in these tutorials doesn’t permit those operations because of limitations in the URLRequest object. It actually throws exceptions if you try to put the .method parameter to anything other than GET or POST. I find this ridiculous. Completely ridiculous. The method of the HTTP Request is a simple string in the HTTP header, 3 or 4 characters long. Why not give us control over it? For Flex users, you can use the HTTPService class, but Flash users? Left to nada. I prefer not using Flex, it’s BLOATED. More on that in another post..

Where we were

At the end of the last tutorial, we had the following code:

Then for the handler function, where we converted the results to XML


Handling Errors

So how many events do the URLLoader throw? And what do they do? Here is a summary :

  • “complete” flash.events.Event.COMPLETEDispatched after all the received data is decoded and placed in the data property of the URLLoader object.
  • “httpStatus” flash.events.HTTPStatusEvent.HTTP_STATUSDispatched if a call to URLLoader.load() attempts to access data over HTTP.
  • “ioError” flash.events.IOErrorEvent.IO_ERROR Dispatched if a call to URLLoader.load() results in a fatal error that terminates the download.
  • “open” Flash.events.Event.OPENDispatched when the download operation commences following a call to the URLLoader.load() method.
  • “progress” flash.events.ProgressEvent.PROGRESSDispatched when data is received as the download operation progresses.
  • “securityError” flash.events.SecurityErrorEvent.SECURITY_ERRORDispatched if a call to URLLoader.load() attempts to load data from a server outside the security sandbox.

I am mostly interested in “complete”, “ioError”, “securityError”, and eventually for requests which return anything other than 200 (201 Created is one example), we need to catch the “httpStatus”. So let’s handle those new events..

For these new error event handlers, we implement the functions. What you do exactly inside those functions depends on the situation, you might want to display an error message, notify another service of the problem, or simply trace them to your output if you are in testing phase. This is what I will demonstrate now.

On with the Singleton

The idea of the Singleton design pattern is to create an object that can be easily accessible from anywhere in your code. You import it, then you do :

and off you go. You will always deal with the same object as this pattern ensures you only have one instance of it. In my head a little bulb lights up and says: this is what you need! Here is the code we need to create our singleton:

Now, it is impossible to have a private or protected constructor. Hence some people came up with nifty tricks to enfore the Singleton; personally I work alone on most projects and don’t need to. You can search on google for “as3 singleton” and you’ll find good examples.

To use the WSClient we would do

But then you might ask, how does my calling code know when the results are ready?

Dispatching Events when the data comes in..

Once the data is handled by our handleResults function, it would be ideal if the WSClient would dispatch an event, telling the calling code that the results are ready to be used. We will therefore derive from EventDispatcher (some people prefer composition) this way:

Now for the dispatching of the event:

Our caller code can now be aware that the data is ready, but how to access it?

Custom Events

The approach I like is to create custom event classes. One such event class is used to notify the caller code that the data is ready and transport the data to it. To do this, we need to derive from the flash.event.Event class. We will define one event type, add a property to contain the data, and define a getter to access it:

On with dispatching the custom event:

Back to the caller code:

Looks like there will be a third installment! I will describe how to handle situations were multiple callers use the same function: how to ensure the results you are getting are yours and not the results from somebody else’s request. I will also standardize certain tasks inside of the WSClient.




Leave a Reply