2011-01-30 74 views
0

我被迫與ActionScript/Adob​​e Air合作。來自Java背景。我無法弄清楚我在這裏做錯了什麼,也許有人可以幫忙。基本上我想這個函數返回它提取的XMLNode爲什麼我不能從函數內部修改這個變量?

public function getXmlWebpage(address:String):XMLNode { 
      var service:HTTPService = new HTTPService(); 
      var xmlResult : XMLNode = null; 

      service.method = "GET"; 
      service.url = address; 
      service.resultFormat = HTTPService.RESULT_FORMAT_XML; 

      function onResult(result:ResultEvent):void{ 
       trace("status code " + result.statusCode); 
       var node : XMLNode = result.result as XMLNode; 
       trace("node has NS URI " + node.namespaceURI); 
       xmlResult = node; 
      } 

      function onFail(event:FaultEvent):void{ 
       trace("fail function of getXmlWebpage called."); 
       Alert.show("error communicating with host " + event.fault.toString()); 
      } 

      service.addEventListener(FaultEvent.FAULT, onFail); 
      service.addEventListener(ResultEvent.RESULT, onResult); 

      service.send(null); 

      trace("return value will be " + xmlResult) 

      return xmlResult; 
     } 

但日誌說(是的,按照這個順序):

return value will be null 
status code 200 
node has NS URI http://www.w3.org/2005/Atom 

什麼我沒有得到嗎?我不能修改xmlResultonResult

回答

3

getXmlWebpage函數不會阻塞等待service.send返回。您無法從此功能返回預期值。而是使用onResult回調到可以發佈和處理結果的東西。

相關問題