Why can't I modify this variable from within the function?
I am bei开发者_如何学Pythonng forced to work with ActionScript / Adobe Air. Coming from a Java-Background. I cannot figure out what I am doing wrong here, maybe someone can help. Basically I would like the function to return the XMLNode
it fetches.
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;
}
But the log says (yes, in that order):
return value will be null
status code 200
node has NS URI http://www.w3.org/2005/Atom
What am I not getting here? Can't I modify xmlResult
from withing onResult
?
the getXmlWebpage function will not block waiting for service.send to return. You cannot return the intended value from this function. Rather use onResult to callback into something that can post and process the result.
精彩评论