等待加载URL以返回数据AS3



我有一个函数,它有一个for循环中的数据,数据被传递到我的函数中,该函数将数据放入URL加载器,加载它,并从加载的URL返回数据。这不起作用,如果我尝试在没有停止的情况下这样做,URL加载速度不够快,无法返回数据。我找不到一种方法来设置止损,这样它就不会产生错误。我不能使用事件监听器,因为它们不能返回值,我不能只是让一个函数将数据分配给一个变量,因为它需要在函数中调用函数与URLloader。我可以做个图表来更好地解释。我不太确定如果这将是可能的,如果某种程度上我需要重组我的代码。我试过这两种方法:这会导致堆栈溢出:

package app
{
    import flash.xml.XMLDocument;
    import flash.net.URLLoader;
    import flash.net.URLRequest;
    import flash.events.Event;
    import flash.display.MovieClip;
    public class main extends MovieClip
    {
        var geoReq:URLRequest = new URLRequest;
        var geoLoader:URLLoader = new URLLoader();
        var GeocodeResponse:XML;
        var geoLocList:XMLList = new XMLList();
        var geoLoc:Array = new Array();
        function main():void
        {
            var urlReq:URLRequest = new URLRequest("http://search.twitter.com/search.atom?q=flu&rpp=3&lang=en&geocode=55.378051,-3.435973,605mi");
            var loader:URLLoader = new URLLoader();
            loader.addEventListener(Event.COMPLETE, getTweets);
            loader.load(urlReq);
        }
        function getTweets( e:Event ):void
        {
            if ( e.target.data )
            {
                var tweets = new Array(); var times = new Array();
                var twitterXML:XML = new XML(e.target.data);
                var tweetList:XMLList = twitterXML.children();
                var tweetItem:String;
                var placeItem:String;
                var tweet:Array = new Array();
                for (var i:int = 0; i < tweetList.length(); i++)
                {
                    tweetItem = tweetList[i].*::title;
                    placeItem = tweetList[i].*::location;
                    if( tweetItem && placeItem != "")
                    {
                    placeItem = loadGeo(placeItem);
                    tweet = [tweetItem, placeItem];
                    tweets.push(tweet);
                    trace(tweets[tweets.length - 1][0]);
                    trace(tweets[tweets.length - 1][1]);
                    }
                }
                trace(tweets.length);
            }
        }
        function loadGeo(loc:String):Array
        {
            geoReq = new URLRequest("https://maps.googleapis.com/maps/api/geocode/xml?address="+loc+"&sensor=false");
            geoLoader.load(geoReq);
            GeocodeResponse = new XML(geoLoader.data);
            geoLoc = returnLoc();
            return geoLoc;
        }
        function returnLoc():Array
        {
            if(geoLoader.data == null)
            {
                returnLoc();
            }
            else
            {
                var returnD:Array = new Array(GeocodeResponse.result.geometry.location[0].lat, GeocodeResponse.result.geometry.location[0].lng);
            }
            return returnD;
        }
    }
}

这会导致错误"TypeError: error #1010: A term is undefined And has no properties "。:

package app
{
    import flash.xml.XMLDocument;
    import flash.net.URLLoader;
    import flash.net.URLRequest;
    import flash.events.Event;
    import flash.display.MovieClip;
    public class main extends MovieClip
    {
        var geoReq:URLRequest = new URLRequest;
        var geoLoader:URLLoader = new URLLoader();
        var GeocodeResponse:XML;
        var geoLocList:XMLList = new XMLList();
        var geoLoc:Array = new Array();
        function main():void
        {
            var urlReq:URLRequest = new URLRequest("http://search.twitter.com/search.atom?q=flu&rpp=3&lang=en&geocode=55.378051,-3.435973,605mi");
            var loader:URLLoader = new URLLoader();
            loader.addEventListener(Event.COMPLETE, getTweets);
            loader.load(urlReq);
        }
        function getTweets( e:Event ):void
        {
            if ( e.target.data )
            {
                var tweets = new Array(); var times = new Array();
                var twitterXML:XML = new XML(e.target.data);
                var tweetList:XMLList = twitterXML.children();
                var tweetItem:String;
                var placeItem:String;
                var tweet:Array = new Array();
                for (var i:int = 0; i < tweetList.length(); i++)
                {
                    tweetItem = tweetList[i].*::title;
                    placeItem = tweetList[i].*::location;
                    if( tweetItem && placeItem != "")
                    {
                    placeItem = loadGeo(placeItem);
                    tweet = [tweetItem, placeItem];
                    tweets.push(tweet);
                    trace(tweets[tweets.length - 1][0]);
                    trace(tweets[tweets.length - 1][1]);
                    }
                }
                trace(tweets.length);
            }
        }
        function loadGeo(loc:String):Array
        {
            geoReq = new URLRequest("https://maps.googleapis.com/maps/api/geocode/xml?address="+loc+"&sensor=false");
            geoLoader.load(geoReq);
            GeocodeResponse = new XML(geoLoader.data);
            geoLoc = [GeocodeResponse.result.geometry.location[0].lat, GeocodeResponse.result.geometry.location[0].lng];
            return geoLoc;
        }
    }
}

请帮忙,提前谢谢,Kyle

您正在等待search.twitter.com请求完成,但是您没有等待geocode请求完成。这是误差。每次使用Loader时,都使用异步等待。

相关内容

  • 没有找到相关文章

最新更新