Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

BlazeDS and ArrayList of custom objects

I'm using BlazeDS to connect Flex with Java. I'm having trouble passing ArrayLists of custom objects from Flex to java.

I have two objects, one is called Category, the other Section. A Category has an ArrayList of Section objects. I can send an ArrayList of Category objects back and forth between Flex and Java, the problem is when I try to access the sections ArrayList of a Category object that has been returned to Java from Flex, I get the following error:

flex.messaging.MessageException: java.lang.ClassCastException : flex.messaging.io.amf.ASObject

For some reason I'm getting an ArrayList of ASObjects rather than my Section objects. I tried looking up how to explicitly type arrays in actionscript, but the only thing I could find was using a Vector object, which BlazeDS does not support. Is it possible to pass an ArrayList of Section objects within an ArrayList of Category objects, or do I have to find another way around?

like image 779
KevMo Avatar asked Apr 16 '09 20:04

KevMo


2 Answers

One of the most common complaints with AS3 is the lack of typed arrays. ArrayLists will only contain objects, you will have to cast the results yourself.

Here is an example of a Java and AS3 class that I would pass around.

In Java:

The top level class:

package mystuff;

public class StuffToSend
{
    public List<Section> sections;
    ...
}

Sections class:

package mystuff;

public class Section
{
    public List<Catagory> categories;
    ...
}

Category class:

package mystuff;

public class Category
{
    ...
}

In AS3:

package mystuff
{
    [RemoteClass(alias="mystuff.StuffToSend")] // So AS3 knows which Java class to map
    public class StuffToSend
    {
        public var sections:ArrayCollection;
        ...
    }
}

package mystuff 
{
    [RemoteClass(alias="mystuff.Section")] // So AS3 knows which Java class to map
    public class Section 
    {
        public var categories:ArrayCollection;
        ...
    }
}

package mystuff 
{
    [RemoteClass(alias="mystuff.Category")] // So AS3 knows which Java class to map
    public class Category
    {
        ...
    }
}  

You can learn more about remoteObjects here: Data Access

like image 154
CookieOfFortune Avatar answered Oct 05 '22 18:10

CookieOfFortune


Flex was actually sending back a flex.messaging.io.ArrayCollection object. Below is the code to convert this to an ArrayList of my java class:

public ArrayList<MyObject> convertArrayCollection(ArrayCollection array){
        ArrayList<MyObject> myObjectArray = new ArrayList();
        ASTranslator ast = new ASTranslator();
        MyObject myObject;
        ASObject aso;

        for (int i=0;i< array.size(); i++){
            myObject = new MyObject();
            aso = new ASObject();

            aso = (ASObject) array.get(i);
            aso.setType("com.myPackage.MyObject");
            myObject = (MyObject) ast.convert(aso, MyObject.class);
            myObjectArray.add(myObject);
        }
        return myObjectArray;
    }
like image 44
KevMo Avatar answered Oct 05 '22 18:10

KevMo