java 自定义对象的 BlazeDS 和 ArrayList

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/758006/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 13:39:09  来源:igfitidea点击:

BlazeDS and ArrayList of custom objects

javaapache-flexactionscript-3blazeds

提问by KevMo

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

我正在使用 BlazeDS 将 Flex 与 Java 连接起来。我在将自定义对象的 ArrayLists 从 Flex 传递到 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:

我有两个对象,一个叫做Category,另一个叫做Section。一个类别有一个 Section 对象的 ArrayList。我可以在 Flex 和 Java 之间来回发送 Category 对象的 ArrayList,问题是当我尝试访问已从 Flex 返回到 Java 的 Category 对象的部分 ArrayList 时,出现以下错误:

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?

出于某种原因,我得到了一个 ASObjects 的 ArrayList 而不是我的 Section 对象。我尝试查找如何在 actionscript 中显式键入数组,但我能找到的唯一方法是使用 BlazeDS 不支持的 Vector 对象。是否可以在 Category 对象的 ArrayList 中传递 Section 对象的 ArrayList,或者我是否必须找到另一种方法?

采纳答案by KevMo

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:

Flex 实际上发回了一个 flex.messaging.io.ArrayCollection 对象。下面是将其转换为我的 java 类的 ArrayList 的代码:

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;
    }

回答by CookieOfFortune

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.

AS3 最常见的抱怨之一是缺少类型化数组。ArrayLists 将只包含对象,您必须自己转换结果。

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

这是我将传递的 Java 和 AS3 类的示例。

In Java:

在 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:

在 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

您可以在此处了解有关 remoteObjects 的更多信息:数据访问

回答by Vladimir

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.

真正的答案是,BlazeDS 是愚蠢的,并且需要类引用来将您的活动脚本对象映射回 Java(即使它刚刚成功地将完全相同的对象从 Java 映射到 AS)。今天我在完全相同的问题上浪费了很多时间。我有很多类似的映射,它们都运行良好,但今天我创建了一个新映射,它开始给我类转换异常。

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.

可能有更好的选择,但我今天已经受够了。