Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/actionscript-3/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/apache-flex/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Actionscript 3 如何在flex中返回HTTP服务响应?_Actionscript 3_Apache Flex_Flex4.5_Httpservice - Fatal编程技术网

Actionscript 3 如何在flex中返回HTTP服务响应?

Actionscript 3 如何在flex中返回HTTP服务响应?,actionscript-3,apache-flex,flex4.5,httpservice,Actionscript 3,Apache Flex,Flex4.5,Httpservice,我很清楚如何在flex中使用HTTP服务,但我想在不同的ActionScript类中分离调用服务和获取服务响应的功能。那么,有人知道如何在flex中返回HTTP服务的响应吗 例如 在实用类中,我希望有一个方法,我将给它一个URL,它将给我从该位置获得的数据。就这样。考虑下面的代码片段。参考代码取自 根据项目的体系结构,有几种解决方案。其主要思想是在服务接收到响应时触发事件(或调用回调),并在调用者中处理它。示例中最简单的方法是在callService方法中返回weatherService对象,并

我很清楚如何在flex中使用HTTP服务,但我想在不同的ActionScript类中分离调用服务和获取服务响应的功能。那么,有人知道如何在flex中返回HTTP服务的响应吗

例如

在实用类中,我希望有一个方法,我将给它一个URL,它将给我从该位置获得的数据。就这样。考虑下面的代码片段。参考代码取自


根据项目的体系结构,有几种解决方案。其主要思想是在服务接收到响应时触发事件(或调用回调),并在调用者中处理它。示例中最简单的方法是在
callService
方法中返回
weatherService
对象,并在调用者中添加相同的侦听器(
ResultEvent.RESULT
FaultEvent.FAULT
)。这个解决方案的缺点是您必须在调用者中解析原始服务器响应,而不是处理一些解析的值对象,但正如我所注意到的,所有这些都取决于您的项目数据流

UPD:回调用法的示例:

//map for storing the {service:callback} linkage
private var callbacks:Dictionary = new Dictionary(true);
/*
callback is a function like: function(success:Boolean, data:Object):void
*/
private function callService(callback:Function):void
{
    var requestObj:Object = {};
    requestObj.q = cityName.text.toString();
    requestObj.format = FORMAT;
    requestObj.num_of_days = cNUMBER_OF_DAYS;
    requestObj.key = API_KEY;

    var weatherService:HTTPService = new HTTPService();
    weatherService.resultFormat = "object";
    weatherService.showBusyCursor = true;
    weatherService.request = requestObj;
    weatherService.addEventListener(ResultEvent.RESULT, weatherService_handler);
    weatherService.addEventListener(FaultEvent.FAULT, weatherService_handler);
    var token:AsyncToken = weatherService.send();

    var obj:Object = {callback:callback, service:weatherService};
    callbacks[token.message.messageId] = obj;
}

protected function weatherService_handler(event:Event):void
{
    var success:Boolean = event.type == ResultEvent.RESULT;
    var token:AsyncToken = success ? ResultEvent(event).token : FaultEvent(event).token;

    var obj:Object = callbacks[token.message.messageId]
    var service:HTTPService = obj.service;
    service.removeEventListener(ResultEvent.RESULT , weatherService_handler);
    service.removeEventListener(FaultEvent.FAULT, weatherService_handler);

    var data:Object = success ? ResultEvent(event).result : FaultEvent(event).fault;
    var callback:Function = obj.callback;
    delete callbacks[event.target];

    callback(success, data);
}

谢谢你的回复。但有了你的解决方案,我的问题也将继续存在。我想要一个完全解耦的类,在这个类中,我只需要给这个方法一个url(以及其他信息,如果有的话),它必须从中获取数据,而这个方法只会返回响应,其他什么都不返回。如果我将返回weatherService对象,那么我将不得不在调用方编写处理程序,这不是我想要做的。你能告诉我你正在使用的另一种解决方案吗?在你的情况下,完全解耦调用者和服务类的最简单方法是创建自定义事件,即ServiceEvent,其中包含data:String和types data and ERROR等字段,但此解决方案缺少同步请求(例如,如果您从调用方向不同的调用方调用callService,则第二个调用方将使用第一个调用方的数据触发)。可以通过将调用方的回调对象/方法传递给callService或为每个请求创建单独的请求服务来解决此问题)你能给出一些代码示例/文档/教程来解释你说的话吗?因为我对flex不太熟悉,所以我能理解你想说什么。谢谢。我真的很感谢你的帮助。你为什么在这里创建字典类型的“回调”?在callservice方法中,requestObj到底在做什么?在weatherService_handler方法中,我对代码了解很少。以及为什么注释掉第二行代码。我想可能是弄错了,对吧?如果您能对代码进行一些解释,这将非常有帮助。我添加了一些注释并修复了一个错误(忘记在回调映射中保存回调函数)。我添加了回调映射来存储对传递给callService方法的回调函数的引用。这是一种字典,因为它能够使用weatherService作为键。使用此代码,您可以从不同的调用者调用callService,还可以为自定义服务请求向callService方法添加其他参数。
//map for storing the {service:callback} linkage
private var callbacks:Dictionary = new Dictionary(true);
/*
callback is a function like: function(success:Boolean, data:Object):void
*/
private function callService(callback:Function):void
{
    var requestObj:Object = {};
    requestObj.q = cityName.text.toString();
    requestObj.format = FORMAT;
    requestObj.num_of_days = cNUMBER_OF_DAYS;
    requestObj.key = API_KEY;

    var weatherService:HTTPService = new HTTPService();
    weatherService.resultFormat = "object";
    weatherService.showBusyCursor = true;
    weatherService.request = requestObj;
    weatherService.addEventListener(ResultEvent.RESULT, weatherService_handler);
    weatherService.addEventListener(FaultEvent.FAULT, weatherService_handler);
    var token:AsyncToken = weatherService.send();

    var obj:Object = {callback:callback, service:weatherService};
    callbacks[token.message.messageId] = obj;
}

protected function weatherService_handler(event:Event):void
{
    var success:Boolean = event.type == ResultEvent.RESULT;
    var token:AsyncToken = success ? ResultEvent(event).token : FaultEvent(event).token;

    var obj:Object = callbacks[token.message.messageId]
    var service:HTTPService = obj.service;
    service.removeEventListener(ResultEvent.RESULT , weatherService_handler);
    service.removeEventListener(FaultEvent.FAULT, weatherService_handler);

    var data:Object = success ? ResultEvent(event).result : FaultEvent(event).fault;
    var callback:Function = obj.callback;
    delete callbacks[event.target];

    callback(success, data);
}