3 Replies Latest reply on Sep 20, 2009 12:18 PM by _gilado

    URLLoader does not work

    _gilado

      I am following the URLLoader example

       

      I'm compiling with -mxmlc -use-network=false Main.as

       

      Both main.swf and the file I'm trying to download (test.txt) reside in the same directory

       

      I get the open even then the HTTPStatus event (status == 0) then nothing happens

       

      I verified that the file is found: I changed the file name to xtest.txt and promptly got an ioError event.

       

      I tried to compile without -use-network=false and the load failed with an error #2148 as expected

       

      I changed the url to http://test.txt, compiled with -use-naetwork=flase and the load failed with error #2028 as expected

       

      I added a timer to make sure the flash is running, and it does

      In short I think it simply is broked!

       

      Compiled using flex sdk "Version 3.3.0 build 4852"

       

      The player's about sends me to http://www.adobe.com/software/flash/about/ ,  which reports "You have version 10,0,32,18 installed"

       

      The URLLoader is suported since player v9

       

      Browser is firefox, and I am running it on Linux

       

       

      Here's the code:

       

      package
      {
          import flash.display.Sprite;
          import flash.text.TextField;
          import flash.utils.setInterval;
          import flash.net.URLRequest;
          import flash.net.URLRequestMethod;
          import flash.net.URLLoader;
          import flash.net.URLLoaderDataFormat;
          import flash.events.IEventDispatcher;
          import flash.events.Event;
          import flash.events.IOErrorEvent;
          import flash.events.HTTPStatusEvent;
          import flash.events.SecurityErrorEvent;
          import flash.utils.ByteArray;

       

          public class Main extends Sprite
          {
              private var downloadUrl:String;
              private var downloadRequest:URLRequest;
              private var downloadLoader:URLLoader;
              private var downloadBytes:ByteArray;
              private var downloadStarted:Boolean = false;
              private var downloadDone:Boolean = false;

       

              private var statusLine:TextField;
              private var statusText:String = "";
              private var timerStep:Number = 100;
              private var elapsedTime:Number = 0;

       

              public function Main()
              {
                  statusLine = new TextField();
                  statusLine.width = 400;
                  statusLine.height = 400;
                  statusLine.multiline = true;
                  statusLine.wordWrap = true;
                  statusLine.text = "Starting";
                  addChild(statusLine);

       

                  // NOTE: Adobe livedocs for setInterval says:
                  // "Instead of using the setInterval() method, consider creating
                  //  a Timer object, with the specified interval, ..."
                  // except it does not work! stick with setInterval() for now
                  elapsedTime = 0;
                  setInterval(showStatus,timerStep);

       

                  download("http://test.txt");
              }

       

              private function showStatus():void
              {
                  var txt:String = "";

       

                  elapsedTime += timerStep;

       

                  if (downloadDone)
                      txt += "Download done ";
                  else
                  if (downloadStarted)
                      txt += "Downloading ";

       

                  txt += statusText + "\n";

       

                  txt += " > " + Math.round(elapsedTime/100)/10 + " < ";

       

                  statusLine.text = txt;
              }

       

              public function download(url:String):void
              {
                  downloadStarted = false;
                  downloadDone = false;
                  downloadUrl = url;
                  downloadRequest = new URLRequest(downloadUrl);
                  downloadLoader = new URLLoader();
                  configureListeners(downloadLoader);
                  try {
                      downloadLoader.dataFormat = URLLoaderDataFormat.TEXT;
                      downloadLoader.load(downloadRequest);
                  } catch (error:Error) {
                      statusText = "download failed: " + error.message;
                  }
              }

       

              private function configureListeners(dispatcher:IEventDispatcher):void
              {
                  dispatcher.addEventListener(Event.COMPLETE,downloadCompletedHandler);
                  dispatcher.addEventListener(Event.OPEN,downloadStartedHandler);
                  dispatcher.addEventListener(SecurityErrorEvent.SECURITY_ERROR,downloadSecurityErrorHandle r);
                  dispatcher.addEventListener(HTTPStatusEvent.HTTP_STATUS,downloadHttpStatusHandler);
                  dispatcher.addEventListener(IOErrorEvent.IO_ERROR,downloadIOErrorHandler);
              }

       

              private function downloadStartedHandler(event:Event):void
              {
                  downloadStarted = true;
                  statusText += "Started " + event;
              }

       

              private function downloadCompletedHandler():void
              {
                  downloadDone = true;
                  statusText += "Completed ";
              }

       

              private function downloadSecurityErrorHandler(event:Event):void
              {
                  statusText += "SecurityError (" + event + ")";
              }
              private function downloadHttpStatusHandler(event:Event):void
              {
                  statusText += "HttpStatus(" + event + ")";
              }
              private function downloadIOErrorHandler(event:Event):void
              {
                  statusText += "IOError (" + event + ")";
              }
          }
      }