首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何通过AsyncToken处理远程方法调用?

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

Stack Overflow用户
提问于 2011-05-25 10:43:26
回答 2查看 4.8K关注 0票数 0

下面是我想要工作的mxml:

代码语言:javascript
复制
<?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>

输出是

Invokehandler呼叫..。

点击处理程序呼叫..。

Resulthandler不被调用,尽管BlazeDS控制台注册了一个成功的结果器。我做错什么了?

编辑:我尝试将这个过程导出到一个类中,这个类应该是用来管理这些事情的。

代码语言:javascript
复制
package argoseye.main

{进口flash.events.Event;

代码语言:javascript
复制
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


}

}

如果我叫它,它就不会到达平地。我的错误在哪里?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2011-05-25 11:47:35

您的错误在于以下几行:

代码语言:javascript
复制
var token:AsyncToken = new AsyncToken();
token.addResponder(new AsyncResponder(onResult,onFault));
token = ro.getCells();

在第1行上创建一个新的令牌,在第2行上分配一个响应程序,然后在第3行重新分配令牌。

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

所以应该是:

代码语言:javascript
复制
var token:AsyncToken = ro.getCells(); 
//ro.getCells() will return a new instance of AsyncToken
token.addResponder(new AsyncResponder(onResult,onFault));
票数 5
EN

Stack Overflow用户

发布于 2011-05-25 10:49:39

使用以下代码:

代码语言:javascript
复制
<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 = ro.getCells();
            token.addResponder(new AsyncResponder(onResult,onFault));
            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"/>

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/6123186

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档