URLLoader class allow to interchange data with the server as text, variables encoded and binary but it donesn’t allow to use encoding options like others channels as RemoteObject in URL access. Nevertheless, it’s possible to use Java serialization capabilities embedding objects (writeObject) in binary format to deserializate it on the client side with Flex (readObject). This operation can be done as following:
- URLLoader object send request by GET/POST to the server waiting for a binary format response.
- Servlet (HttpServlet) get request and its parameters.
- Build the object to send.
- Use Amf3Output class from Flex Messaging libraries given with BlazeDS to convert the object in binary format AMF and write it in the response.
- Flex client get the response as ByteArray and deserialize it with the readObject() method.
Here are client and server codes of the process. (BlazeDS libraries must be included in the server project):
SerializeServlet.java
package com.servlet;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import flex.messaging.io.SerializationContext;
import flex.messaging.io.amf.Amf3Output;
@SuppressWarnings("serial")
public class SerializeServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException {
processRequest(req, res);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException {
processRequest(req, res);
}
@SuppressWarnings("unchecked")
protected void processRequest(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
try {
List object = new ArrayList();
object.add("Mercury");
object.add("Venus");
object.add("Earth");
object.add("Mars");
object.add("Jupiter");
object.add("Saturn");
object.add("Uranus");
object.add("Neptune");
object.add("Pluto");
byte[] data = getBytesAMF(object);
ServletOutputStream out = response.getOutputStream();
out.write(data);
} catch (Exception e) {
e.printStackTrace();
}
}
public static byte[] getBytesAMF(Object obj) throws java.io.IOException {
SerializationContext context = SerializationContext.getSerializationContext();
Amf3Output amf3Output = new Amf3Output(context);
ByteArrayOutputStream bos = new ByteArrayOutputStream();
amf3Output.setOutputStream(bos);
amf3Output.writeObject(obj);
amf3Output.flush();
amf3Output.close();
byte[] data = bos.toByteArray();
return data;
}
public static byte[] getBytes(Object obj) throws java.io.IOException {
ByteArrayOutputStream bos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(bos);
oos.writeObject(obj);
oos.flush();
oos.close();
bos.close();
byte[] data = bos.toByteArray();
return data;
}
}
MySerialize.mxlm
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute"
creationComplete="onCreationComplete()">
<mx:Script>
<![CDATA[
import mx.collections.ArrayCollection;
import mx.rpc.events.ResultEvent;
[Bindable]
private var myPlanet:ArrayCollection;
private function onCreationComplete():void {
mySend();
}
private function mySend():void {
var loader:URLLoader=new URLLoader();
loader.dataFormat=URLLoaderDataFormat.BINARY;
loader.addEventListener(Event.COMPLETE,onComplete);
loader.load(new URLRequest('http://localhost:8400/DynamicWeb/serialize'));
}
private function onComplete(event:Event):void {
var data : ByteArray = ByteArray(event.target.data);
var obj : Object = data.readObject();
myPlanet=ArrayCollection(obj);
}
]]>
</mx:Script>
<mx:VBox width="100%" height="100%" horizontalAlign="center" verticalAlign="middle">
<mx:List dataProvider="{myPlanet}" width="200"/>
</mx:VBox>
</mx:Application>







Freelance
Downloads
Projects
Utilities
Friends
Links
January 25, 2009
7:05 pm
Categories
Files






January 26, 2009 - 11:45 am -
Script for other output formats:
XMLNAME ID “);
ServletOutputStream out = response.getOutputStream();
response.setContentType(”application/binary”);
out.println(”
Object
ObjectOutput out = new ObjectOutputStream(response.getOutputStream());
out.writeObject(object);
out.close();
Binary
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(baos);
oos.writeObject(object);
oos.flush();
byte[] serializedBytes = baos.toByteArray();