Working with Flex RPC Services in ActionScript Mobile Projects

In a previous post I showed you how you can use the QNX UI components in a Flex Mobile Project in order to use 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 is working 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. Evertything 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):

[SWF(height="600",width="1024")]
public class PlaybookRPC extends Sprite {

   registerClassAlias("flex.messaging.messages.RemotingMessage", RemotingMessage);
   registerClassAlias("flex.messaging.messages.CommandMessage",CommandMessage);
   registerClassAlias("flex.messaging.messages.AcknowledgeMessage",
           AcknowledgeMessage);
   registerClassAlias("flex.messaging.messages.ErrorMessage", ErrorMessage);
   registerClassAlias("DSC", CommandMessageExt);
   registerClassAlias("DSK", AcknowledgeMessageExt);
   registerClassAlias("flex.messaging.io.ArrayList", ArrayList);
   registerClassAlias("flex.messaging.config.ConfigMap", ConfigMap);
   registerClassAlias("flex.messaging.io.ArrayCollection", ArrayCollection);
   registerClassAlias("flex.messaging.io.ObjectProxy", 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);
   registerClassAlias("flex.messaging.messages.MessagePerformanceInfo",
           MessagePerformanceInfo);

   public function PlaybookRPC() {
      super();

      // 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);
token.addResponder(responder);

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 not working.

Show Comments

Hide Comments

Comments are closed.