Archive for category actionscript
Java byte[] to ActionScript String - UPDATED
Posted by brianr in actionscript, blazeds, java on July 30th, 2009
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.
Wordpress Plugin SyntaxHighlighter Test On ActionScript 3
Posted by brianr in actionscript, wordpress on June 29th, 2009
This is a test post to determine if I like SyntaxHighlighter as a source code plugin for Wordpress. Here’s two ActionScript 3 examples:
private var foo:Bar = new Bar();
/**
* Web App Solution Confidential Information
* Copyright 2009, Web App Solution, Inc.
*
* @author Brian Riley
* @date June, 29, 2009
*/
package com.wasi.test
{
import com.wasi.test.interfaces.IFooBar;
/**
* This class rocks my world.
*/
public class Foo extends Bar implements IFooBar
{
/**
* Constant happy value.
*/
public var myLongString:String = "this is a really, really long string to illustrate what happens when one line of code is tooooo long";
/**
* Constant happy value.
*/
private static const MY_CONST:String = "myPrivateStaticConstant";
/**
* Constructor.
*
* param myFoo A String vlaue that's happy.
* param myBar A Number vlaue that's sad.
*/
public function Foo(myFoo:String, myFoo:Number)
{
// TODO
}
}
}
Just wrap your source code in the following syntax:
[sourceCodeAlias]
my source code
[/sourceCodeAlias]
where sourceCodeAlias = as3 or actionscript3. Here’s the full list of possible code syntax highlighters at your disposal.
ActionScript ArrayCollection Binding and Event Dispatching
Posted by brianr in actionscript, flex on April 22nd, 2009
We hear this one a lot, so I’m throwing it out there as a reminder for new and old Flex and ActionScript developers…
You’ll get some new collection data say from a remote service and you simply update your model level ArrayCollection (”AC”) property by doing:
myModel.myAC = responseFromRemoteService.myNewAC;
And low and behold the objects that are either binding to it or listening for the change event don’t do anything??? What? How the? But I debugged it and I saw that the new data was getting stored in my model-level AC…how come this thing isn’t working? Stoopid Flex…bleh.
Well, it’s simply because setting the original AC to a new AC doesn’t implicitly fire off any change events (which is the underlying implementation for data binding as well.) In order to make this function as expected, one simply needs to set the source property on an AC like so:
myModel.myAC.source = responseFromRemoteService.myNewAC.source;
The source property in an AC is not only the underlying Array for an AC, but also fires off that change event (I believe it’s the listChanged type event) that’s so important to binding and anyone else listening for changes on that AC. Truth be told, this is actually done in the underlying ListCollectionView class that an AC inherits from, but that’s just details.