I recently needed to send a Java byte[] to Flex that ultimately needed to be converted to an ActionScript String on the Flex side. I wrestled with it for a bit before I realized that I needed to make the byte[] on the Java side encoded with UTF-8.
Here’s a quick EchoService I created in Java that takes a String parameter from Flex, converts it into a byte[], and then sends it back to Flex, as well as the accompanying Flex code that handles the service’s result and transforms the byte[] back into a String in ActionScript.
package com.wasi.services.test;
import java.io.UnsupportedEncodingException;
public class EchoService
{
public String echo(String param)
{
return param;
}
public int echo(int param)
{
return param;
}
public byte[] echoStringToByte(String param)
{
System.out.println("echoStringToByte: " + param);
byte[] byteArray = null;
try
{
byteArray = param.getBytes("UTF-8");
}
catch (UnsupportedEncodingException e)
{
e.printStackTrace();
}
return byteArray;
}
}
And on the Flex side:
import mx.rpc.remoting.mxml.RemoteObject;
import mx.rpc.events.FaultEvent;
import mx.rpc.events.ResultEvent;
private var echoStringToByteService:RemoteObject;
private function init():void
{
trace("init");
this.echoStringToByteService = new RemoteObject();
this.echoStringToByteService.destination = "echoService";
this.echoStringToByteService.endpoint = "http://localhost:8080/bds/messagebroker/amf";
this.echoStringToByteService.showBusyCursor = true;
this.echoStringToByteService.addEventListener(ResultEvent.RESULT, onEchoStringToByteServiceResult);
this.echoStringToByteService.addEventListener(FaultEvent.FAULT, onEchoStringToByteServiceFault);
}
private function onEchoServiceSubmit(evt:MouseEvent):void
{
trace("onEchoServiceSubmit");
var str:String;
str = this.echoStringTextInput.text;
this.echoStringToByteService.echoStringToByte(str);
}
private function onEchoStringToByteServiceResult(evt:ResultEvent):void
{
trace("onEchoStringToByteServiceResult");
var byteArray:ByteArray;
byteArray = evt.result as ByteArray;
// both the readUTFBytes() and toString() methods give return you the string
trace("onEchoStringToByteServiceResult readUTFBytes = " + byteArray.readUTFBytes(byteArray.bytesAvailable));
trace("onEchoStringToByteServiceResult toString = " + byteArray.toString());
this.echoServiceResultText.text = byteArray.readUTFBytes(byteArray.bytesAvailable);
}
private function onEchoStringToByteServiceFault(evt:FaultEvent):void
{
trace("onEchoStringToByteServiceFault");
}
In order to run this you’ll need to:
- Copy and paste the ActionScript listed above into a Flex Application tat calls the init() method on creationComplete.
- Have BlazeDS or LCDS running on a server (as you can see in the definition of my RemoteObject in Flex I created a small Java web-app with a context root of /bds).
- Configure remoting-config.xml by adding in the following destination node listed below.
<destination id="echoService"> <properties> <source>com.wasi.services.test.EchoService</source> <scope>application</scope> </properties> </destination>
NOTE: I removed a couple things for clarity and this code has not been retested to verify that it still runs, but you should get the idea.
#1 by Sadineni - September 9th, 2009 at 18:29
Thanks for the post!!!
Anil.
#2 by Vajahat Ali Niazi - November 3rd, 2009 at 03:08
Nice Post Buddy!