在ActionScript 3中,我需要一些异步事件的帮助,我正在编写一个简单的类,它有两个函数,这两个函数都是返回字符串(下面概述的逻辑和代码)。由于AS3 HTTPService的异步性质,返回值行总是在从服务返回结果之前到达,从而产生一个空字符串。是否有可能在此函数中包含某种类型的逻辑或语句,使其在返回值之前等待响应?有处理这类事情的框架吗?
interest
公共函数geocodeLocation(地址:字符串):点{//调用Google地理代码服务,直接通过httpService:HTTPService =新HTTPService;httpService.useProxy =假;httpService.url = //"URL将在这里“;httpService.method = HTTPRequestMessage.GET_METHOD;var AsyncToken : asyncToken = httpService.send();asyncToken.addResponder( asyncToken.addResponder( onResult,onFault));函数onResult( e: ResultEvent,token : Object = null ):void {/解析JSON和get值,逻辑尚未实现var jsonValue:String="“}函数onFault( info : Object : Object = null ):void{Alert.show(info.toString();}返回jsonValue;//行达到onResult fires }
发布于 2009-11-22 20:18:41
您应该在应用程序中定义onResult和onFault --无论您在哪里调用geocodeLocation --然后在地址之后将它们作为参数传递到函数中。您的onResult函数将接收数据,解析Point并对其进行处理。您的geocodeLocation函数不会返回任何内容。
public function geocodeLocation(address:String, onResult:Function, onFault:Function):void
{
//call Google Maps API Geocode service directly over HTTP
var httpService:HTTPService = new HTTPService;
httpService.useProxy = false;
httpService.url = //"URL WILL GO HERE";
httpService.method = HTTPRequestMessage.GET_METHOD;
var asyncToken : AsyncToken = httpService.send();
asyncToken.addResponder( new AsyncResponder( onResult, onFault));
}然后在你的应用程序中
function onResult( e : ResultEvent, token : Object = null ) : void
{
var jsonValue:String=""
//parse JSON and get value, logic not implemented yet
var point:Point = new Point();
//do something with point
}
function onFault( info : Object, token : Object = null ) : void
{
Alert.show(info.toString());
//sad face
}
var address:String = "your address here";
geocodeLocation(address, onResult, onFault);当web服务响应时,控件将传递给您的onResult函数,在那里您将解析Point并对其做一些有用的事情,或者传递给您的onFault函数。
顺便说一下,这样调用Google地理编码器可能会遇到问题,最好使用官方SDK并利用它们的代码:http://code.google.com/apis/maps/documentation/flash/services.html
https://stackoverflow.com/questions/1744839
复制相似问题