2013-01-14 66 views
2

我很清楚如何在Flex中使用HTTP服務,但我想分開調用服務的功能並獲得不同ActionScript類中服務的響應。那麼有沒有人知道我如何返回HTTP服務在Flex中的響應?如何在flex中返回HTTP服務響應?

例如,

在實用工具類我想有一個方法,我會給一個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"); 
     } 

回答

3

根據項目的架構,有幾種解決方案。主要思想是在服務接收響應時觸發事件(或調用回調)並在調用者中處理它。在您的示例中,最簡單的方法是返回callService方法中的weatherService對象,並在調用方中添加相同的偵聽器(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); 
} 
+0

感謝您的答覆。但對於您的解決方案,我的問題仍將存在。我想完全解耦的類,我只需要給方法url(以及其他信息,如果有的話)從中獲取數據,並且方法將返回我只是響應,沒有別的。如果我將返回weatherService對象,那麼我將不得不在處理函數中編寫處理程序,這不是我想要做的。你能否給我提示你正在使用的另一種解決方案。 – ankurtr

+1

在你的情況下,完全解耦調用者和服務類的最簡單方法是創建自定義事件,即帶_data:String_和_DATA_和_ERROR_類型的字段的_ServiceEvent_,但此解決方案缺少同時請求(例如,如果您從_callService_中調用_callService_對於不同的呼叫者,第二個呼叫者將被從第一個呼叫者的數據觸發)。通過將回調對象/方法從調用者傳遞給_callService_或爲每個請求創建單獨的請求服務,可以解決此問題。) – fsbmain

+0

您可以提供任何代碼示例/文檔/教程來解釋您所說的內容,因爲我不熟悉彎曲,以便我可以理解你想說什麼。 – ankurtr

相關問題