Working with AMF in ActionScript 3
Thursday February 5th, 2009For anyone not aware, Action Message Format (AMF) is a data transfer format that allows you to make remote service calls with ActionScript. The ability to speak to external servers from within Flash or Flex allows you to develop much richer applications. For instance you could use AMF in conjunction with a CMS to load in dynamic content every time your Flash movie plays. The Slide Show widget that I have previously showcased, makes extensive use of AMF to load in settings for the application (colors, behaviors etc.) as well as the content that will be displayed. In this tutorial I will show you the basics of AMF including how to make calls and handle successful and erroneous results.
In order to make use of AMF you not only need to write ActionScript code, you also need to have access to an external API that supports the format. Because of that unique requirement, I will focus mainly on code snippets and general guidelines that you should be able to apply to your specific projects.
Assuming you have access to an AMF supported API, the basics of your remote call are as follows:
var _netConnection:NetConnection = new NetConnection();
_netConnection.connect(remotingGateway);
var _connectionResponder:Responder = new Responder(successfulResult, erroneousResult);
_netConnection.call("serviceClass.serviceMethod", connectionResponder);
The code above consists of 3 main parts:
- The actual call in handled via a NetConnection Object. In order to access the remote service, you will need to use NetConnection.connect() to connect to a gateway URL. The gateway information as well as the service classes and methods should all be accessible to you in the documentation for the remote call.
- A Responder Object is used to handle the results of the remote call. The parameters for the Responder constructor need to be functions that will handle a successful result or an erroneous one respectively. For the sake of clarity, I have created the Responder on a separate line, but I usually define the Responder in NetConnection.call() as follows:
_netConnection.call( "serviceClass.serviceMethod", new Responder(successfulResult, erroneousResult));
- The final piece of the puzzle is the performing the actual call to the remote service. The NetConnection.call() method requires the service call as a parameter and the responder object to handle the results.
The example above works great unless of course you need to provide parameters to your remote call. How does that work? Read on.
In order to send parameters to the remote call, you will need to define an array of parameters and pass that array as a parameter with NetConnection.call(). For example:
var _netConnection:NetConnection = new NetConnection();
_netConnection.connect(remotingGateway);
var parameters:Array = new Array();
parameters[0] = param1;
parameters[1] = param2;
parameters[2] = param3;
_netConnection.call("serviceClass.serviceMethod", new Responder(successfulResult, erroneousResult), parameters);
The example above is a more general purpose solution for making remote calls.
Now that you have successfully made your remote call, you’ll need to handle the results. To do so, make sure you have defined the two methods you passed to Responder constructor as follows:
function successfulResult(obj:Object):void function erroneousResult(obj:Object):void
When the server responds to your call, it will return the results inside of a generic object that you can then parse using the dot operator as you would to access class members. The service you are calling should provide supporting documentation on what format to expect your results in and how to parse out the relevant information. Ultimately inside of an object, the results could be formatted in any manner and I wont assume that formatting for the purposes of this tutorial.
Below is a complete, robust solution that you can work from:
//Required imports
import flash.net.NetConnection;
import flash.net.Responder;
import flash.events.NetStatusEvent;
var remotingGateway:String = "http://www.somedomain.com/gateway";
var _netConnection:NetConnection = new NetConnection();
//Use NetStatus event to handle an incorrect gateway URL.
_netConnection.addEventListener(NetStatusEvent.NET_STATUS, connectionHandler):
_netConnection.connect(remotingGateway);
//Call parameters
var parameters:Array = new Array();
parameters[0] = param1;
parameters[1] = param2;
parameters[2] = param3;
//Make the call
_netConnection.call("serviceClass.serviceMethod", new Responder(successfulResult, erroneousResult), parameters);
/**
* Outputs error if an invalid gateway is used.
*/
function connectionHandler(ev:NetStatusEvent):void
{
if(ev.info.code == "NetConnection.Call.Failed")
{
trace("Unable to find gateway");
}
}
/**
* Handle successful results
*/
function successfulResult(obj:Object):void
{
trace("Call Successful");
}
/**
* Handle failed results
*/
function erroneousResult(obj:Object):void
{
trace("Call Failed");
}
Armed with the above information, you should now be able to make remote calls using ActionScript 3!
hi,
great Post! I found your post, as I am trying to get connected to my gateway, and still have issues! I am using ColdFusion, and in the past I have always used “http://localhost/flashservices/gateway/” However, using your example I get a “Call Failed”
I stripped down the code, no parameters, jst looking for a simple your connected or it failed.
also on line 10 should it read:
_netConnection.addEventListener(NetStatusEvent.NET_STATUS, connectionHandler); I am using “;” and not “:”
thanks so much!
Johnny