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

标签 actionscript-3 apache-flex flex4.5 httpservice

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

例如

在 UTILITY 类中,我想要一种方法,我将为其提供一个 URL,它将为我提供从该位置获得的数据。就是这样。考虑以下代码片段。引用代码取自 could not be able to create http service programmitically in flex

        private function callService():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.url = BASE_URL;
            weatherService.resultFormat = "object";
            weatherService.showBusyCursor = true;
            weatherService.request = requestObj;
            weatherService.addEventListener(ResultEvent.RESULT , weatherService_resultHandler);
            weatherService.addEventListener(FaultEvent.FAULT, weatherService_faultHandler);
            weatherService.send();
        }

        protected function weatherService_resultHandler(event:ResultEvent):void
        {
            trace("got result");
            **//WANT TO GIVE THIS RESULT BACK TO THE CALLER. SINCE RETURN TYPE OF 
            //METHOD IS VOID I CANNOT RETURN ANYTHING FROM HERE. HOW TO MAKE THIS
            //METHOD TO RETURN DATA?**
        }

        protected function weatherService_faultHandler(event:FaultEvent):void
        {
            trace("got fault");
        }

最佳答案

根据项目的架构,有多种解决方案。主要思想是在服务收到响应时触发事件(或调用回调)并在调用者中处理它。在您的示例中,最简单的方法是返回 weatherService callService 中的对象方法并在调用者中添加相同的监听器( ResultEvent.RESULTFaultEvent.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);
}

关于actionscript-3 - 如何在flex中返回HTTP服务响应?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14320256/

相关文章:

actionscript-3 - 将 NFC 读取器与 Adob​​e AIR 结合使用

actionscript-3 - 将所有错误定向到文本字段

flash - 在要进行 GC 的对象中将引用设置为 null?

apache-flex - 如果按钮有焦点,如何让按钮不会在空格键上触发?

css - 如何在 Flex 应用程序运行时加载并应用 CSS 样式表?

flash - 滚动文本延迟中的 Flex Mobile textInput

flash - 如何减少我的flash 游戏的后台引擎600MB 内存使用高峰?

apache-flex - 激活 Flex 自动化库

flex4.5 - 在 FlexMojos 4.0-RC2 和 Flex SDK 4.5.1.21328 上构建时获取 "Error: unable to resolve '/assets/images/**.png' 进行转码

apache-flex - Adobe Flex 4.5 Spark : Binding ItemRenderer Component to Parent