BlazeDS and ArrayList custom objects

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

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

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

For some reason, I get ArrayList ASObjects, not section objects. I tried to figure out how to explicitly type arrays in actionscript, but the only thing I could find was to use a Vector object that BlazeDS does not support. Is it possible to pass ArrayList objects from a section in category ArrayList objects, or do I need to find another way?

+4
source share
3 answers

Flex actually sent back the 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; } 
+4
source

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

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

In Java:

Top level class:

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

Partition 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 { ... } } 

Learn more about deleted objects here: Data Access

+4
source

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

found the answer here: Link

in your case, the solution will be:

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

there may be better options, but enough for me today.

0
source

All Articles