DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Kalatravas has posted 29 posts at DZone. View Full User Profile

Actionscript 3 : Reading Name / Value Pairs From An External File With The URLLoader Class

09.16.2010
| 2711 views |
  • submit to reddit
        //The following example loads and displays the data found in a local text file. It also traces event handling information. 
Note: To run this example, put a file named urlLoaderExample.txt in the same directory as your SWF file. That file should only contain the following line of text: answer=42&question=unknown 

The example code does the following:

The constructor function creates a URLLoader instance named loader and a URLRequest instance named request, which contains the location and name of the file to be loaded. 
The loader object is passed to the configureListeners() method, which adds listeners for each of the supported URLLoader events. 
The request object is then passed to loader.load(), which loads the text file. 
When the URLLoader has finished loading the text file the Event.COMPLETE event fires, triggering the completeHandler() method. The completeHandler() method creates a URLVariables object from the text loaded from the file. The URLVariables object converts URL-encoded name/value pairs into ActionScript properties to make it easier to manipulate the loaded data. 

// www.tournasdimitrios1.wordpress.com

package {
    import flash.display.Sprite;
    import flash.events.*;
    import flash.net.*;

    public class URLLoaderExample extends Sprite {
        public function URLLoaderExample() {
            var loader:URLLoader = new URLLoader();
            configureListeners(loader);

            var request:URLRequest = new URLRequest("urlLoaderExample.txt");
            try {
                loader.load(request);
            } catch (error:Error) {
                trace("Unable to load requested document.");
            }
        }

        private function configureListeners(dispatcher:IEventDispatcher):void {
            dispatcher.addEventListener(Event.COMPLETE, completeHandler);
            dispatcher.addEventListener(Event.OPEN, openHandler);
            dispatcher.addEventListener(ProgressEvent.PROGRESS, progressHandler);
            dispatcher.addEventListener(SecurityErrorEvent.SECURITY_ERROR, securityErrorHandler);
            dispatcher.addEventListener(HTTPStatusEvent.HTTP_STATUS, httpStatusHandler);
            dispatcher.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);
        }

        private function completeHandler(event:Event):void {
            var loader:URLLoader = URLLoader(event.target);
            trace("completeHandler: " + loader.data);
    
            var vars:URLVariables = new URLVariables(loader.data);
            trace("The answer is " + vars.answer);
        }

        private function openHandler(event:Event):void {
            trace("openHandler: " + event);
        }

        private function progressHandler(event:ProgressEvent):void {
            trace("progressHandler loaded:" + event.bytesLoaded + " total: " + event.bytesTotal);
        }

        private function securityErrorHandler(event:SecurityErrorEvent):void {
            trace("securityErrorHandler: " + event);
        }

        private function httpStatusHandler(event:HTTPStatusEvent):void {
            trace("httpStatusHandler: " + event);
        }

        private function ioErrorHandler(event:IOErrorEvent):void {
            trace("ioErrorHandler: " + event);
        }
    }
}