我很清楚如何在 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.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);
}
关于actionscript-3 - 如何在flex中返回HTTP服务响应?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14320256/