HTTPService AsyncToken和AsyncResponder示例

时间:2010-01-11 09:42:36

标签: flex asynchronous httpresponse httpservice asynctoken

在哪里可以找到实现由AsyncToken和AsyncResponder异步调用的HTTPService的Flex应用程序示例? 提前致谢

httpservice以一定的频率发送这样的字符串:

行#列#号#行#列#号#行#列#号#....

编辑代码:

<?xml version="1.0" encoding="utf-8"?>
<mx:Application creationComplete="onCreationComplete()" 
xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute">
<mx:Script>
<![CDATA[
    import mx.rpc.remoting.RemoteObject;
    import mx.rpc.events.ResultEvent;
    import mx.rpc.events.FaultEvent;
    import mx.rpc.http.mxml.HTTPService;
    import mx.rpc.AsyncRequest;
    import mx.rpc.AsyncResponder;
    import mx.rpc.AsyncToken;
    import mx.collections.ArrayCollection;
    import mx.controls.advancedDataGridClasses.AdvancedDataGridColumn;
    import mx.controls.AdvancedDataGrid;
    import mx.controls.Alert;
    import mx.rpc.IResponder;


    [Bindable]
    public var dataList:ArrayCollection;

    public function getResults(source:String) : ArrayCollection {
    var ac:ArrayCollection = new ArrayCollection();
    var data:Array = source.split('#');
    for (var i:int = 0; i < data.length; i += 3) {
    var dataObj:Object = {row: data[i], column: data[i+1], value: data[i+2]};     
    ac.addItem(dataObj)
    }

    return ac;
    }
    public function result(event:ResultEvent):void{
    dataList = getResults( String(event.result) );
    }
    public function fault(event:FaultEvent) : void {
    dataList = getResults(String(event.fault)); 
    }

public function onCreationComplete():void
{
var service:HTTPService = new HTTPService();
service.url = "http://10.15.20.75/server4flex/servlet/Datagen";
service.resultFormat = "text";
var token:AsyncToken = service.send(dataList);
token.addResponder(new mx.rpc.Responder(result, fault));
}

]]>
</mx:Script>
<mx:AdvancedDataGrid id="dg"
dataProvider="{result}"  
liveScrolling="true"  
    x="10" y="10" width="621"
    verticalScrollPolicy="on"
 >
        <mx:columns>
                    <mx:AdvancedDataGridColumn dataField="row"
           headerText="Riga"/>
                    <mx:AdvancedDataGridColumn dataField="column"
           headerText="Colonna"/>
                    <mx:AdvancedDataGridColumn dataField="value" 
           headerText="Valore"/>
        </mx:columns>
 </mx:AdvancedDataGrid>

 </mx:Application>

2 个答案:

答案 0 :(得分:6)

我不知道你在照顾什么,但herehere是使用AsyncTokenAsyncResponder

的方法

修改

  1. 您的dataList必须是Bindable
  2. 不要在每次循环迭代中设置dataList
  3. 在结果准备就绪时,您必须在某个时刻给您打电话getResults
  4. {li} event result函数中的EventResultEvent,还有result,其中包含您的数据的[Bindable] public var dataList:ArrayCollection; public function getResults(source:String) : ArrayCollection { var ac:ArrayCollection = new ArrayCollection(); var data:Array = source.split('#'); for (var i:int = 0; i < data.length; i += 3) { ac.addItem( {row: data[i], column: data[i+1], value: data[i+2]} ); } return ac; } private function result(event:ResultEvent) : void { dataList = getResults( String(event.result) ); } 字段

    这可能看起来像这样(未经测试):

    <?xml version="1.0" encoding="utf-8"?>
    <mx:Application creationComplete="onCreationComplete()"
                xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute">
    <mx:Script>
    <![CDATA[
      import mx.collections.ArrayCollection;
      import mx.rpc.AsyncToken;
      import mx.rpc.Responder;
      import mx.rpc.events.FaultEvent;
      import mx.rpc.events.ResultEvent;
      import mx.rpc.http.mxml.HTTPService;
    
        [Bindable]
        public var dataList : ArrayCollection;
    
        public function getResults(source : String) : ArrayCollection {
            var ac : ArrayCollection = new ArrayCollection();
            var data : Array = source.split('#');
            for (var i : int = 0; i < data.length; i += 3) {
                var dataObj : Object = {row: data[i], column: data[i + 1], value: data[i + 2]};
                ac.addItem(dataObj)
            }
    
            return ac;
        }
    
        public function result(event : ResultEvent) : void {
            dataList = getResults(String(event.result));
        }
    
        public function fault(event : FaultEvent) : void {
            //here do whatever you want to manage the error you received
        }
    
        public function onCreationComplete() : void
        {
            var service : HTTPService = new HTTPService();
            service.url = "http://127.0.0.1/getDatas.php";
            service.resultFormat = "text";
            var token : AsyncToken = service.send();
            token.addResponder(new mx.rpc.Responder(result, fault));
        }
    
    ]]>
    </mx:Script>
    <mx:AdvancedDataGrid id="dg"
                         dataProvider="{dataList}"
                         liveScrolling="true"
                         x="10" y="10" width="621"
                         verticalScrollPolicy="on"
            >
        <mx:columns>
            <mx:AdvancedDataGridColumn dataField="row"
                                       headerText="Riga"/>
            <mx:AdvancedDataGridColumn dataField="column"
                                       headerText="Colonna"/>
            <mx:AdvancedDataGridColumn dataField="value"
                                       headerText="Valore"/>
        </mx:columns>
    </mx:AdvancedDataGrid>
    
    </mx:Application>
    

    <强> EDIT2

    这是一个使用简单的php文件来获取在本地Web服务器上运行的数据的工作示例。

      

    Flex部分

    <?php print "1#c1#v1#2#c2#v2#3#c3#v3"?>
    
      

    Php part(getDatas.php)

    {{1}}

答案 1 :(得分:4)

private function onCreationComplete():void
{
    var service:HTTPService = new HTTPService();
    service.url = "http://www.google.com";
    service.resultFormat = "text";
    var token:AsyncToken = service.send();
    token.addResponder(new mx.rpc.Responder(result, fault));
}

private function result(event:Event):void
{
    trace(1, event);
}

private function fault(event:Event):void
{
    trace(2, event);
}

找到here