1

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

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

行#column#number#row#column#number#row#column#number#....

编辑代码:

<?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>
4

2 回答 2

6

我不知道你真正在看什么,但在这里这里,例如,是使用AsyncTokenAsyncResponder

编辑:

  1. dataList必须是Bindable
  2. 不要dataList在每个循环迭代上设置
  3. getResults当你的结果准备好时,你必须在某个时候调用你的函数
  4. eventin resultfunction 是一个Event但也是一个包含您的数据ResultEvent的字段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) );
}

编辑2

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

挠性部分

<?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>

php部分(getDatas.php)

<?php print "1#c1#v1#2#c2#v2#3#c3#v3"?>
于 2010-01-11T10:13:43.110 回答
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);
}

在这里找到。

于 2010-01-11T19:23:52.353 回答