views:

789

answers:

1

Hello,

I need a little help with asynchronous events in ActionScript 3. I am writing a simple class that has two functions, both of which return strings(logic and code outlined below). Due to the asynchronous nature of the AS3 HTTPService, the return value line is always reached before a result is returned from the service, yielding an empty string. Is it possible to include some type of logic or statement in this function that will make it wait for a response before returning a value? Is there a framework that handles this type of stuff?

  1. Call service
  2. Parse JSON result, isolate value of interest
  3. Return Value

    public function geocodeLocation(address:String):Point
    {
     //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
    {
     //parse JSON and get value, logic not implemented yet
     var jsonValue:String=""
    }
    
    
    function onFault( info : Object, token : Object = null ) : void
    {
     Alert.show(info.toString());
    }
    
    
    return jsonValue; //line reached before onResult fires
    
    }
+2  A: 

You should define onResult and onFault in your app – wherever you call geocodeLocation – and then pass them into your function as arguments after the address. Your onResult function will receive the data, parse the Point and do something with it. Your geocodeLocation function won't return anything.

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));
}

And then in your app somewhere:

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);

When the web service responds, control will pass either to your onResult function, where you will parse the Point and do something useful with it, or to your onFault function.

BTW you might run into problems calling the Google Maps geocoder this way, it's probably better to use the official SDK and take advantage of their code: http://code.google.com/apis/maps/documentation/flash/services.html

RandomEtc