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.

2
  • 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). -- amfphp.sourceforge.net Commented Mar 1, 2011 at 21:57
  • What AMF Library are you using for JavaScript. CAn you share some code to reproduce the problem? Commented Mar 1, 2011 at 22:35

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

2 Comments

+1 for ingenuity. This should work assuming that the weird character is not an important part of the AMF packet.
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.
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();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.