自定义对象的 BlazeDS 和 ArrayList [英] BlazeDS and ArrayList of custom objects
问题描述
我正在使用 BlazeDS 将 Flex 与 Java 连接起来.我在将自定义对象的 ArrayLists 从 Flex 传递到 java 时遇到问题.
I'm using BlazeDS to connect Flex with Java. I'm having trouble passing ArrayLists of custom objects from Flex to java.
我有两个对象,一个叫做Category,另一个叫做Section.一个类别有一个 Section 对象的 ArrayList.我可以在 Flex 和 Java 之间来回发送 Category 对象的 ArrayList,问题是当我尝试访问已从 Flex 返回到 Java 的 Category 对象的部分 ArrayList 时,出现以下错误:
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
出于某种原因,我得到了一个 ASObject 的 ArrayList,而不是我的 Section 对象.我尝试查找如何在 actionscript 中明确键入数组,但我唯一能找到的是使用 Vector 对象,而 BlazeDS 不支持该对象.是否可以在 Category 对象的 ArrayList 中传递 Section 对象的 ArrayList,还是我必须找到其他方法?
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?
推荐答案
Flex 实际上发回了一个 flex.messaging.io.ArrayCollection 对象.下面是将其转换为我的 java 类的 ArrayList 的代码:
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;
}
这篇关于自定义对象的 BlazeDS 和 ArrayList的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!