Passing an Array or Object using Flash AS3 Remoting? Don’t Forget the Object Encoding!
May 20, 2008
Earlier today I posted about an issue I was having when attempting to pass a structure to a ColdFusion web service using AS3 netConnection. Whenever I attempted to pass a structure to the web service, the call would be rejected before it even reached the method it was calling. The error I was receiving was
“Unknown object type tag (17)”
The Adobe documentation insisted this was possible but I tried everything I could think of with no success. There were a few developers sharing my misery but their calls for help went unanswered, and by extension, so did mine.
After spending the better part of two days tooling around with the code and searching the web, I finally hit on the solution. It derived from a combination of Oscar Trelles’ post on AS3 Flash Remoting and a post on laflash.org about AS3 and Flash Media Server. In order to pass a structure to a ColdFusion component, I had to set the netConnection’s objectEncoding to AMF0 (the default is AMF3).
I’m guessing that this qualifies as a bug since AMF0 is supposed to be for serializing AS1 and AS2 objects and AMF3 is supposed to be optimized for AS3 objects. While I was able to send simple objects, like strings and integers, to the web service using the default setting, I ran into a wall when sending an Array, Object, or Dictionary type.
Until this issue is addressed, if you’re going to be sending complex objects to a ColdFusion web service, make sure to set the object encoding to AMF0. Here’s a simple example that sends a structure containing a string to a web service. The web service then returns the string:
AS3 CODE
var myService = new NetConnection()
////////////////////////////////////////////////
//set encoding to AMF0 for complex objects
myService.objectEncoding = 0;
///////////////////////////////////////////////
myService.connect("http://www.yourdomain.com/flashservices/gateway")
var responder = new Responder(getTest_Result, onFault);
var mystruct:Array = new Array();
mystruct.mystring = "foo";
myService.call("testAPI.test", responder, mystruct);
function getTest_Result(result){
trace("success: "+ result);
}
function onFault( f){
trace("There was a problem: " + f.description);
}
COLDFUSION WEB SERVICE:
<pre>
<cfcomponent>
<cffunction name="test" output="no" access="remote" returntype="any" >
<cfargument name="argstruct" type="any" />
<cfset mystr1 =argstruct["mystring"]>
<cfreturn mystr1 />
</cffunction>
</cfcomponent>
</pre>
If you’re REALLY interested in objectEncoding, you can read up on it in Adobe’s livedocs .
I hope this post saves someone the two days I just blew figuring it out!
-rG
Posted in 
content rss
July 8th, 2008 at 10:59 pm
Brilliant!
I never would’ve suspected ObjectEncoding as the problem. Thanks for saving the time. Question is, how does OE affect AMFPHP applications? Same issue?
WL
July 30th, 2008 at 4:10 pm
GREAT POST!!! Thank you sooo much!
January 5th, 2009 at 2:34 am
THANK YOU VERY MUCH
I really very much obliged for your tips , I am sracthing my head for past three days and never thought about this way….
You Rock!!!!!!
Saurabh
January 17th, 2009 at 10:45 pm
Thanks so much - my code finally works. Now I have to figure out why!
July 11th, 2009 at 5:41 pm
Nice save… pat yourself on the back!
August 16th, 2009 at 8:16 pm
In amfphp this is my service:
function sendBA()
{
$bar = new ByteArray(file_get_contents(”someSWFfile.swf”));
return $bar;
}
in Flash cs4 As3 that`s my code:
import flash.net.*;
import flash.utils.ByteArray;
var myConnection:NetConnection = new NetConnection();
myConnection.objectEncoding=0;
myConnection.connect(”http://localhost/amfphp/gateway.php”);
var res:Responder=new Responder(onResult,onFault);
var ldr = new Loader();
function onResult(responds:*):void {
var bA:ByteArray = responds as ByteArray;
ldr.contentLoaderInfo.addEventListener(Event.COMPLETE, completeHandler);
ldr.loadBytes(bA);
}
function completeHandler(e:Event):void {
addChild(ldr.content);
}
function onFault(responds:ByteArray):void {
trace(”Connection problem”);
}
myConnection.call(”MyClass.sendBA”, res);
for this I get the following error(no matter what AMF I`m using - 0 or 3 or no objectEncoding at all):
TypeError: Error #2007: Parameter bytes must be non-null.
at flash.display::Loader/_loadBytes()
If instead I switch the `onResult` function code to the following:
function onResult(responds:ByteArray):void {
var bA:ByteArray = responds;
ldr.contentLoaderInfo.addEventListener(Event.COMPLETE, completeHandler);
ldr.loadBytes(bA);
}
I get the following error(no matter what AMF I`m using - 0 or 3 or no objectEncoding at all):
Error #1034: Type Coercion failed: cannot convert Object@bed95d9 to flash.utils.ByteArray.
Any idea what is wrong ?
August 17th, 2009 at 2:36 am
Problem solved. It was AMF3 after all. Sorry and thanks.
November 7th, 2009 at 2:45 pm
I was about to throw my computer out of the window when I got here. Thank you!