3

I am using ExternalInterface in Flex to retrieve AMF encoded string from Javascript. The problem is the AMF encoded string sometimes contains \u0000 which causes the ExternalInterface to return null instead of the encoded string from Javascript.

Any idea how to solve this?

Thanks in advance.

doorman
  • 15,707
  • 22
  • 80
  • 145
  • are you using AMF via Flash Remoting or via something like AMFPHP? I've only ever used AMF as a service (never had to use ExternalInterface). -- http://amfphp.sourceforge.net/ – Jacksonkr Mar 01 '11 at 21:57
  • What AMF Library are you using for JavaScript. CAn you share some code to reproduce the problem? – JeffryHouser Mar 01 '11 at 22:35

2 Answers2

4

The \0000 is falsely interpreted as EOF when reading external data. The same thing happens when it appears in XML files, as well.

You should be able to replace it with an unambiguous character sequence before passing the string to Flash, and back upon reception in ActionScript. In the JavaScript function, use something like

return returnString.replace (/\0000/g, "{nil}");

This should remove the unwanted \0000 characters from the string before returning it to Flash.

On the Flash side, use

receiveString = receiveString.replace (/\{nil\}/g, "\u0000"); 

directly after receiving the data.

weltraumpirat
  • 22,544
  • 5
  • 40
  • 54
  • +1 for ingenuity. This should work assuming that the weird character is not an important part of the AMF packet. – JeffryHouser Mar 01 '11 at 23:05
  • Yes the \u0000 is part of the AMF message. I have tried replacing it with another character and back to \u0000 in Flex but without luck. I am using Google App Engine to push AMF message to Javascript and then to Flex. The message is encoded to AMF3 using pyamf on GAE site and it looks correct in Javascript but fails when passing it to Flex through ExternalInterface. The PyAMF encoding uses \u0000 when passing in DateTimeProperty or IntegerProperty. StringProperty is not encoded with \u0000 so one solution would be to change all class properties to string but I don´t like to do that. – doorman Mar 02 '11 at 00:46
2

Encoding the pyamf AMF output to base64 will do the trick.

Here is the encoding part in python:

encoder = pyamf.get_encoder(pyamf.AMF3)
encoder.writeObject(myObject)
encoded = base64.b64encode(encoder.stream.getvalue())

Here is the decoding part in AS3:

var myDecoder:Base64Decoder = new Base64Decoder();
myDecoder.decode(base64EncodedString);
var byteArr:ByteArray = myDecoder.toByteArray()
byteArr.position = 0;
var input:Amf3Input = new Amf3Input();
input.load(byteArr);                
var test:MyObject = input.readObject();
doorman
  • 15,707
  • 22
  • 80
  • 145