Working with Flex RPC Services in ActionScript Mobile Projects

In a previous post I showed you how you can use  QNX UI components in a Flex Mobile project that uses Flex RPC services (REST Style Services/Web Services/Remoting). What if you want to use Flex RPC services in an ActionScript Mobile Project? It isn’t possible, isn’t it? Wrong, you can make it work with a little bit of extra work.

This works because, as my colleague Renaun Erickson said, Flex != MXML. In other words if you add the right parts from the Flex framework to your project you will be able to reuse these Flex services. Though, you won’t be able to write MXML code. Everything will be just ActionScript code.

Once you created a new ActionScript Mobile Project (these project supports Android and BlackBerry Tablet OS as target platforms) there are two steps you have to complete to enable Flex RPC support.

Step 1. Right-click on the project name and choose Properties. Then click on the ActionScript Build Path entry. You will notice there are already some parts of the Flex framework added.

Expand the first entry (Flex Hero) and add these two SWCs: framework.swc and rpc.swc (on my computer these SWCs are in /Applications/AdobeBeta/Adobe Flash Builder Burrito/sdks/4.5.0/frameworks/libs). Then you have to add the bundles for the default locale. So, this time, click on the “Add SWC Folder…” button and add the locale folder from Flex SDK (on my computer this is the path: /Applications/AdobeBeta/Adobe Flash Builder Burrito/sdks/4.5.0/frameworks/locale/en_US).

Once you apply these changes your project should compile with no errors.

Step 2. You have to do some of the work the Flex compiler does for you automatically when you work with Flex projects. Remember, this project is an ActionScript project. You have to register a number of classes used by the AMF3 protocol. If you don’t do this, HTTPService will work, but when you try to use RemoteObject (this method uses AMF3 for encoding the messages) you will get this error:

TypeError: Error #1034: Type Coercion failed: cannot convert Object@3779ee1 to mx.messaging.messages.ErrorMessage.

Fortunately, this is an easy fix. You can register the classes needed at the top of your ActionScript application (in my project this class is named PlaybookRPC):

public class PlaybookRPC extends Sprite {

   registerClassAlias("flex.messaging.messages.RemotingMessage", RemotingMessage);
   registerClassAlias("flex.messaging.messages.ErrorMessage", ErrorMessage);
   registerClassAlias("DSC", CommandMessageExt);
   registerClassAlias("DSK", AcknowledgeMessageExt);
   registerClassAlias("", ArrayList);
   registerClassAlias("flex.messaging.config.ConfigMap", ConfigMap);
   registerClassAlias("", ArrayCollection);
   registerClassAlias("", ObjectProxy);

   // You may want to register pub/sub and other rpc message types too...
   registerClassAlias("flex.messaging.messages.HTTPMessage", HTTPRequestMessage);
   registerClassAlias("flex.messaging.messages.SOAPMessage", SOAPMessage);
   registerClassAlias("flex.messaging.messages.AsyncMessage", AsyncMessage);
   registerClassAlias("DSA", AsyncMessageExt);

   public function PlaybookRPC() {

      // support autoOrients
      stage.align = StageAlign.TOP_LEFT;
      stage.scaleMode = StageScaleMode.NO_SCALE;


Now, you are ready to use Flex RPC services in this project. You can write something along these lines if you want to use Remoting:

var remoting:mx.rpc.remoting.RemoteObject = new RemoteObject("AuthorsService");
remoting.source = "AuthorsService";
remoting.endpoint = BASE_URL + "remoting/gateway.php";
var token:AsyncToken = remoting.getData();
var responder:Responder = new Responder(onResult, onFault);

You can download my project from here (server side services are not included). I haven’t tried Web Services (WSDL/SOAP) or Messaging (Producer/Consumer) but I don’t see any reason for them not to work.

3 thoughts on “Working with Flex RPC Services in ActionScript Mobile Projects

  1. Thanks for sharing. Trying on Android works fine for http GET method. I need to use http POST with custom data. No luck so far, any tips/experience?


  2. Pingback: Working with Flex Services and BlackBerry Tablet OS SDK for Adobe AIR : Mihai Corlan

  3. If I use remote object in a pure as3 project and want to connect and get data from a drupal webservice.. I always get Channel.Connect.Failed error null url: ‘null'” . Whats up with that?

    and instead of using
    I use
    remoting.getOperation(“operation name”).send(…)

Leave a Reply