BlazeDS and ArrayList of custom objects
Asked Answered
H

3

5

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?

Haakon answered 16/4, 2009 at 20:53 Comment(0)
H
4

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;
    }
Haakon answered 20/4, 2009 at 19:11 Comment(0)
L
4

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

Linares answered 16/4, 2009 at 21:8 Comment(9)
Would I cast it on the AS side or java side? Can you point me in the direction of any examples?Haakon
Thanks for the example. That's what my code looks like, the problem happens when I pass the ArrayList of Category objects from java to actionscript, then back to java. I get the class cast exception on the following line of code in my java: categories.get(1).getSections();Haakon
Can you provide the variable declarations? I can't tell why that would cause an error if you do not provide me the types. Maybe you didn't declare the generics correctly?Linares
Here is my Category class in AS3: package my.package { import mx.collections.ArrayCollection; [Bindable] [RemoteClass(alias="my.package.Category")] public class Category { public function Category() { } ... public var sections:ArrayCollection; } } And Section: package my.package { import mx.collections.ArrayCollection; [Bindable] [RemoteClass(alias="my.package.Section")] public class Section { public function Section() { } ... public var sectionName:String; } }Haakon
here's my java classes: public class Category implements Serializable { ... private ArrayList<Section> sections; } public class Section implements Serializable { ... private String sectionName; }Haakon
It's private, set them to public. It's a known issue that Flex will only pick up public members. so public ArrayList<Section> sections. Then it should be picked up correctly on the other end.Linares
That's a bummer. My Category and Section classes are entities and the variables cannot be public.Haakon
Hmmm... you're going to have to find a way to convert an ASObject(essentially a hashmap) to Section then... not sure how that's setup in your code though.Linares
I guess that will be my task Monday morning :) Thanks for the helpHaakon
H
4

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;
    }
Haakon answered 20/4, 2009 at 19:11 Comment(0)
D
1

the real answer is, that BlazeDS is stupid, and requires class reference to map your active script object back into Java (even if it just successfully mapped exactly the same object from Java to AS). I wasted quite some time on exactly the same problem today. I had quite a few similar mappings and they all worked fine, but today I created a new one, and it started to give me class cast exception.

found an answer here: Link

in your case solution would be:

package mystuff
{
    [RemoteClass(alias="mystuff.Section")] 
    public class Section
    {
        private var stupidBlazeDs : Category;
        public var categories:ArrayCollection;
    ...
    }
}

there might be better options but I had enough for today.

Durning answered 7/6, 2011 at 15:11 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.