2

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

例如

在 UTILITY 类中,我想要一种方法,我会给它一个 URL,它会给我从那个位置获得的数据。就是这样。考虑以下代码片段。取自的参考代码无法在 flex 中以编程方式创建 http 服务

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

1 回答 1

3

根据项目的架构,有几种解决方案。主要思想是在服务收到响应并在调用者中处理它时触发事件(或调用回调)。您的示例中最简单的方法是在方法中返回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);
}
于 2013-01-14T14:40:20.327 回答