HTTPService AsyncToken 和 AsyncResponder 示例

发布于 2024-08-17 09:18:21 字数 2410 浏览 12 评论 0原文

在哪里可以找到 Flex 应用程序的示例,该应用程序实现由 AsyncToken 和 AsyncResponder 异步调用的 HTTPService? 预先感谢

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

row#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>

Where can I find an example of Flex application which implements an HTTPService asynchronously called by an AsyncToken and an AsyncResponder?
Thanks in advance

the httpservice send a string like this with a certain frequency:

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

EDITED CODE:

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

七堇年 2024-08-24 09:18:21

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

< strong>编辑:

  1. 你的dataList必须是Bindable
  2. 不要在每次循环迭代时设置dataList
  3. 你必须调用你的函数当结果准备就绪时,getResults
  4. result 函数中的event 是一个 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) );
}

Edit2

这是一个使用简单的 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"?>

I dont know what you are really looking after but here or here for example, are way to use AsyncToken and AsyncResponder

Edit:

  1. your dataList have to be Bindable
  2. Don't set dataList on each loop iteration
  3. You have to call you function getResults at some point when your results are ready
  4. event in result function is an Event but also a ResultEvent where there is a result field containing your data

Which may look as this (untested):

[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) );
}

Edit2:

this is a working example using a simple php file to get the data running on a local web server.

Flex part

<?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 part (getDatas.php)

<?php print "1#c1#v1#2#c2#v2#3#c3#v3"?>
对风讲故事 2024-08-24 09:18:21
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);
}

找到此处

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);
}

Found here.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文