Apache flex 如何通过AsyncToken处理远程方法调用?

Apache flex 如何通过AsyncToken处理远程方法调用?,apache-flex,remoteobject,asynctoken,Apache Flex,Remoteobject,Asynctoken,下面是我想要使用的mxml: <?xml version="1.0" encoding="utf-8"?> <s:Application xmlns:fx="http://ns.adobe.com/mxml/2009" xmlns:s="library://ns.adobe.com/flex/spark" xmlns:mx="library://ns.adobe.com/flex/mx" minWidth="955

下面是我想要使用的mxml:

<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009" 
               xmlns:s="library://ns.adobe.com/flex/spark" 
               xmlns:mx="library://ns.adobe.com/flex/mx" minWidth="955" minHeight="600">

    <fx:Script>
        <![CDATA[
            import argoseye.main.Golem;

            import mx.collections.ArrayCollection;
            import mx.rpc.AsyncResponder;
            import mx.rpc.AsyncToken;
            import mx.rpc.Responder;
            import mx.rpc.events.FaultEvent;
            import mx.rpc.events.InvokeEvent;
            import mx.rpc.events.ResultEvent;
            import mx.rpc.remoting.RemoteObject;
            protected function button1_clickHandler(event:MouseEvent):void
            {
                var ro:RemoteObject = new RemoteObject("destination");
                ro.endpoint = "http://Jesus/blazeds/messagebroker/amf";
                ro.addEventListener(InvokeEvent.INVOKE,onInvoke);

                var token:AsyncToken = new AsyncToken();
                token.addResponder(new AsyncResponder(onResult,onFault));

                token = ro.getCells();
                textfeld.text = textfeld.text + "Clickhandler called .... \n";

            }

            public function onResult(event:ResultEvent,token:Object):void {
                textfeld.text = textfeld.text + "Resulthandler called .... \n";
                var cellList:ArrayCollection = event.result as ArrayCollection;

            }

            public function onFault(event:FaultEvent,token:Object):void
            {
                textfeld.text = textfeld.text + "Faulthandler called .... \n";
            }

            public function onInvoke(event:InvokeEvent):void {
                textfeld.text = textfeld.text + "Invokehandler called .... \n";
            }
        ]]>
    </fx:Script>

    <fx:Declarations>
        <!-- Place non-visual elements (e.g., services, value objects) here -->
    </fx:Declarations>
    <s:Button x="1093" y="575" label="Button" click="button1_clickHandler(event)"/>
    <s:TextArea x="1022" y="183" id="textfeld"/>
</s:Application>
{ 导入flash.events.Event

import mx.collections.ArrayCollection;
import mx.messaging.ChannelSet;
import mx.messaging.channels.AMFChannel;
import mx.rpc.AsyncResponder;
import mx.rpc.AsyncToken;
import mx.rpc.events.FaultEvent;
import mx.rpc.events.ResultEvent;
import mx.rpc.remoting.RemoteObject;

public class Schem
{
    public var info:String="";


    public function Schem()
    {       
    }

    public function loadCurrentSchem():void
    {
        var ro:RemoteObject = new RemoteObject("Hibernatetest");
        ro.endpoint = "http://jesus/blazeds/messagebroker/amf";

        var token:AsyncToken = ro.getCells();
        token.addResponder(new AsyncResponder(onResult,onFault));

        info = info + "Loader Called ...";


    }

    public function onResult(event:ResultEvent,token:Object):void {
        var cellList:ArrayCollection = event.result as ArrayCollection;
        info = info + "Resulthandler Called";

    }

    public function onFault(event:FaultEvent,token:Object):void
    {

    }
    //Eventhandlers


    //Getters, Setters


}
}


如果我调用它,它不会到达eventhandler。我的错误在哪里?

使用以下代码:



您的错误在于以下几行:

var token:AsyncToken = new AsyncToken();
token.addResponder(new AsyncResponder(onResult,onFault));
token = ro.getCells();
您正在第1行创建一个新令牌。 您在第2行分配了一个响应者。 然后在第3行重新分配令牌

您在第3行所做的是有效地创建一个新令牌,因此它没有附加响应程序,因为它是一个新实例

因此,它应该是:

var token:AsyncToken = ro.getCells(); 
//ro.getCells() will return a new instance of AsyncToken
token.addResponder(new AsyncResponder(onResult,onFault));

我认为问题在于ro.getCells()函数返回的AsyncToken与new创建的AsyncToken对象完全不同。因此,事件侦听器没有添加到令牌中,并且您没有获得正确的行为。非常有启发性!非常感谢。我不太清楚
token=ro.getCells()的深层含义
var token:AsyncToken = new AsyncToken();
token.addResponder(new AsyncResponder(onResult,onFault));
token = ro.getCells();
var token:AsyncToken = ro.getCells(); 
//ro.getCells() will return a new instance of AsyncToken
token.addResponder(new AsyncResponder(onResult,onFault));