0

私はJava+Flexを使用してプロジェクトを行っています。Javaクラスを作成し、Flexリモートオブジェクトを使用してメソッドを呼び出しました。すべてのコードをmxmlに記述すると、正常に実行されます。しかし、スクリプトをasファイルでラップすると、何か奇妙なことがあります。リモートオブジェクトによって返される結果を取得するには、Flexボタンを2回クリックする必要があります。私のasファイルに何か問題があると思います。

以下は私の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="500" minHeight="600">
<fx:Declarations>
    <mx:RemoteObject id="Control" destination="Control" showBusyCursor="true" />
</fx:Declarations>

<fx:Script>
    <![CDATA[
        import com.wntime.ControlUtil;
        import mx.rpc.events.ResultEvent;

        private var resultOfCmd:String;
        private var cmdStr:String;
        private var ct:ControlUtil = new ControlUtil();

        /* invoke as method */
        private function test():void
        {
            cmdStr = cmdTxt.text;
            resultOfCmd = ct.exec(cmdStr);
            cmdConsole.text = resultOfCmd;
        }

        /*  */
        private function exec():void{
            cmdStr = cmdTxt.text;
            Control.execCmd(cmdStr);
            Control.addEventListener(ResultEvent.RESULT,execCmd_clickHandler);
        }

        private function execCmd_clickHandler(event:ResultEvent):void
        {
            cmdConsole.text = event.result.toString();

        }

        private function clearCmdConsole():void
        {
            cmdConsole.text = "";
        }

    ]]>
</fx:Script>

<s:Panel id="CmdPanel" x="70" y="50" width="501" height="350" title="Command Execute Panel">
    <s:RichText x="20" y="33" fontSize="14" text="Cmd:"/>
    <s:TextInput id="cmdTxt" x="60" y="30" width="239"/>
    <s:Button id="execCmd" x="312" y="30" width="68" label="execute" click="exec()"/>
    <s:Button x="400" y="30" label="CmdTest" click="test()"/>
    <s:TextArea id="cmdConsole" x="20" y="85" width="450" editable="false"/>
    <s:Button x="400" y="250" label="clear" click="clearCmdConsole()"/>
</s:Panel>
</s:Application>

ControlUtilという名前のasファイルは次のとおりです。

package com.wntime{
import mx.rpc.events.FaultEvent;
import mx.rpc.events.ResultEvent;
import mx.rpc.remoting.RemoteObject;

public class ControlUtil
{
    private var cmd:String = null;
    private var result:String = null;
    private var roControl:RemoteObject = new RemoteObject();

    public function ControlUtil()
    {
        roControl.destination = "Control";
    }

    public function exec(_cmd:String):String{
        this.cmd = _cmd;
        roControl.execCmd(cmd);
        roControl.addEventListener(FaultEvent.FAULT, execCmd); 
        roControl.addEventListener(ResultEvent.RESULT, execCmd);
        return result;
    }

    public function execCmd(event:ResultEvent):void
    {
        setResult(event.result.toString());
    }

    public function setResult(_result:String):void
    {
        this.result = _result;
    }

    }
}

実行ボタンをクリックすると。結果はコンソール(テキストエリア)に直接表示されます。ただし、結果をコンソールに表示するには、CmdTestボタンを2回クリックする必要があります。

手を差し伸べてください。よろしくお願いします。

4

2 に答える 2

1

これは単なる推測ですが、Java側で呼び出すメソッドは、リスナーを追加するよりも速く戻ると思います。したがって、イベントハンドラーは呼び出されません。2回目はすべてのリスナーが配置され、通話が成功します。リモートメソッドを呼び出す前に、リスナーを追加してみてください。

于 2012-03-23T09:15:41.203 に答える
0

私の意見では、あなたのコードにはさまざまなエラー/問題があります。私はそのようなことをします:

package com.wntime{
import mx.rpc.events.FaultEvent;
import mx.rpc.events.ResultEvent;
import mx.rpc.remoting.RemoteObject;

public class ControlUtil
{
    private var cmd:String = null;
    private var result:String = null;
    private var roControl:RemoteObject = new RemoteObject();
    // the callBack function is the function that is called when the 
    // remoteobject successfully or not complete the request... 
    // you can set as parameters anything you want...
    private var callBack:Function = null;
    public function ControlUtil()
    {
        roControl.destination = "Control";
    }

    public function exec(callBack:Function, _cmd:String):void{
        this.cmd = _cmd;
        this.callBack = callBack;
        roControl.addEventListener(FaultEvent.FAULT, errorCmd); 
        roControl.addEventListener(ResultEvent.RESULT, execCmd);
        roControl.execCmd(cmd);
    }

    private function execCmd(event:ResultEvent):void
    {           
        callBack(true,event.result.toString());
    }

    private function errorCmd(event:FaultEvent):void
    {
        callBack(false, event.error); // call the callBack function passing the value you need
    }  

}
}

callBack 関数は次のようなものです。

private function name(b:Boolean, s:String = null){....}

* 編集 *

メインコードからexecコマンドを呼び出します...

// function invoked when the button is clicked!
private function buttonClick():void
{
    var tmp:ControlUtil = new ControlUtil();
    //exec(callBack:Function, _cmd:String)
    //you pass the function as a reference so when the async request is terminated the function is invoked and you can parse the result....
    tmp.exec(getResult, "cmqString");
}

// callBack function for the method ControlUtil.exec
private function getResult(b:Boolean, result:String = ""):void
{
    if (b)
    {
       // the call returned correctly and the result variable contains the value.
    }
    else
    {
       // the call failed and the result variable contains the error
    }
}

使用時に ControlUtil で指定したため、ブール値と結果値の両方が返されますcallBack(true/false, result/error)

好きなように関数を作成できます...

于 2012-03-23T19:57:59.637 に答える