Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Adobe AIR Development

Reply
Contributor
Posts: 10
Registered: ‎01-13-2011
My Device: Not Specified

How get multiple URLs in order?

[ Edited ]

I have a program that needs to call two separate urls to get data.   I would like to load URLs in the order they are called, as they add data to the same data provider.

 

I am using separate URLLoaders and URLRequests for each call and am getting intermittent IO errors. I tried reusing the URLLoader but then it only loads data from one URL.

 

From searching, it appears that synchronous URL requests are not supported in AS3.

 

I rather not chain the calls, in case I need to add another URL to call from.

 

Is there a proper way to do this?

 

TIA

Developer
Posts: 1,094
Registered: ‎12-29-2010
My Device: PlayBook, Z10 LE, Dev Alpha C

Re: How get multiple URLs in order?

I don't know if this is the "proper" way to do it, but maybe it would work:

 

Create individual intermediate variables to temporarily hold the responses from each request.

 

Create an event listener that listens for the completion of each request and tracks whether they have all completed.  When they are all done, sort your response data as needed and insert it into your final variable.

 

Developer
Posts: 165
Registered: ‎10-26-2010
My Device: BlackBerry® PlayBook™
My Carrier: .

Re: How get multiple URLs in order?

[ Edited ]

Was working on a code sample, but 

 

This is just a quick example that can be refactored into a class that meets your needs or as a few functions in your application. It creates a loop using Event.COMPLETE's listener function. When all of the URLs have finished, it fires a final event (ALL_DONE) where you can process the data and be assured everything is available.

package
{
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.net.URLLoader;
	import flash.net.URLRequest;
	
	/**
	 * ...
	 * @author Chris
	 */
	public class URLLoaderExample extends Sprite
	{
		static public const ALL_DONE:String = "allDone";
		
		private var urlLoader:URLLoader;
		private var urlArray:Array = ['http://www.google.com', 'http://www.yahoo.com', 'http://www.amazon.com'];

		private var data:Array = [];
		private var counter:uint;
		
		public function URLLoaderExample() 
		{
			counter = 0; //reset counter away from the looping part of the class
			urlLoader = new URLLoader();

			urlLoader.addEventListener(Event.COMPLETE, loadURL);
			addEventListener(ALL_DONE, onAllDone);
			
			//call loadURL to start loading the first URL
			loadURL();
		}
		
		private function loadURL(e:Event = null):void 
		{
			if (e && e.target.data) { data.push(e.target.data); }
			
			
			if (urlArray[counter])
			{
				trace ("*executing* >> [URLLoaderExample.loadURL] " + urlArray[counter]);
				urlLoader.load(new URLRequest(urlArray[counter]));
				counter++;
			}
			else
			{
				dispatchEvent(new Event(ALL_DONE));
			}
		}
		
		private function onAllDone(e:Event):void 
		{
			trace ("*executing* >> [URLLoaderExample.onAllDone] ");
			urlLoader.removeEventListener(Event.COMPLETE, loadURL);
			removeEventListener(ALL_DONE, onAllDone);
			
			//process data
			trace(String(data[2]).substr(300, 1000));
		}		
	}

}