Flash / Flex / ActionScript/Network/URLLoader

Материал из Web эксперт
Перейти к: навигация, поиск

Accessing the Object That Registered the Listener

 
package {
  import flash.display.*;
  import flash.net.*;
  import flash.events.*;
  public class Main extends Sprite {
    public function Main(  ) {
      var urlLoader:URLLoader = new URLLoader(  );
      urlLoader.addEventListener(Event.ruPLETE, completeListener);
      urlLoader.load(new URLRequest("someFile.txt"));
    }
    private function completeListener (e:Event):void {
      trace(e.currentTarget == e.target);  // Displays: true
    }
  }
}



Accessing the Target Object

 
package {
  import flash.display.*;
  import flash.net.*;
  import flash.events.*;
  public class Main extends Sprite {
    public function Main(  ) {
      var urlLoader:URLLoader = new URLLoader(  );
      urlLoader.addEventListener(Event.ruPLETE, completeListener);
      urlLoader.load(new URLRequest("someFile.txt"));
    }
    private function completeListener (e:Event):void {
      trace("Load complete");
    }
  }
}



Add IOErrorEvent, HTTPStatusEvent, SecurityErrorEvent and Event.COMPLETE to URLLoader

 
package {
  import flash.events.*;
  import flash.net.*;
  import flash.display.*;
  public class Main extends Sprite{
    public function Main(  ) {
      var loader:URLLoader = new URLLoader(  );
      loader.addEventListener( IOErrorEvent.IO_ERROR, handleIOError );
      loader.addEventListener( HTTPStatusEvent.HTTP_STATUS, handleHttpStatus );
      loader.addEventListener( SecurityErrorEvent.SECURITY_ERROR, handleSecurityError );
      loader.addEventListener( Event.ruPLETE, handleComplete );
     // loader.dataFormat = DataFormat.VARIABLES;
      loader.load( new URLRequest( "example.txt" ) );
    }
    function handleIOError( event:IOErrorEvent ):void {
      trace( "Load failed: IO error: " + event.text ); 
    }
    function handleHttpStatus( event:HTTPStatusEvent ):void {
      trace( "Load failed: HTTP Status = " + event.status );
    }
    function handleSecurityError( event:SecurityErrorEvent ):void {
      trace( "Load failed: Security Error: " + event.text );
    }
    function handleComplete( event:Event ):void {
      trace( "The data has successfully loaded" );
    }
}}



Checking Load Progress

 
package {
  import flash.display.*;
  import flash.text.*;
  import flash.events.*
  import flash.net.*;
  
  public class Main extends Sprite {
    private var _output:TextField = new TextField( );
  
    public function Main(  ) {
      _output.width = stage.stageWidth;
      _output.height = stage.stageHeight;
      
      addChild( _output );    
      var loader:URLLoader = new URLLoader(  );
      
      loader.addEventListener( ProgressEvent.PROGRESS, handleProgress );
      
      loader.load( new URLRequest( "example.txt" ) );
    }
    
    private function handleProgress( event:ProgressEvent ):void {
      var percent:Number = Math.round( event.bytesLoaded 
                                       / event.bytesTotal * 100 );
      
      _output.text = " Loaded: " + event.bytesLoaded + "\n"
                        + "  Total: " + event.bytesTotal + "\n"
                        + "Percent: " + percent;
    }
  }
}



Display LoadBar

 
package
{
    import flash.display.Sprite;
    import flash.events.*;
    import flash.net.*;
    public class Main extends Sprite
    {
        private var loader:URLLoader = new URLLoader(new URLRequest("http://example.ru/huge.xml"));
        private var total:Sprite = new Sprite();
        private var loaded:Sprite = new Sprite();
    
        public function Main()
        {
            total.graphics.beginFill(0xff0000, 1);
            total.graphics.drawRect(0, 0, 200, 10);
            total.graphics.endFill();
            addChild(total);
            total.y = 200;
            total.x = 100;
    
            addChild(loaded);
            loaded.y = 200;
            loaded.x = 100;
    
            loader.addEventListener(Event.OPEN, openListener);
            loader.addEventListener(Event.ruPLETE, completeListener);
            loader.addEventListener(ProgressEvent.PROGRESS, progressListener);
        }
    
        private function openListener(event:Event):void
        {
            trace(" opened " + loader.bytesLoaded + " but nothing loaded yet ");
        }
    
        private function completeListener(event:Event):void
        {
            trace(" all done loading " + loader.data + " and here"s the xml file we loaded ");
        }
    
        private function progressListener(event:Event):void
        {
            var amount:Number = (loader.bytesLoaded / loader.bytesTotal) * 200;
            loaded.graphics.clear();
            loaded.graphics.beginFill(0x00ff00, 1);
            loaded.graphics.drawRect(0, 0, amount, 10);
            loaded.graphics.endFill();
        }
    }
}



Event Listener Registration Examples

 
package {
  import flash.display.*;
  import flash.net.*;
  import flash.events.*;
  public class Main extends Sprite {
    public function Main(  ) {
      var urlLoader:URLLoader = new URLLoader(  );
      urlLoader.addEventListener(Event.ruPLETE, completeListener);
      urlLoader.addEventListener(IOErrorEvent.IO_ERROR, ioErrorListener);
      urlLoader.load(new URLRequest("someFile.txt"));
    }
    private function completeListener (e:Event):void {
      trace("Load complete");
    }
    private function ioErrorListener (e:Event):void {
      trace("Error loading file.");
    }
  }
}



Events and Event Handling for URLLoader

 
package {
  import flash.display.*;
  import flash.net.*;
  import flash.events.*;
  public class Main extends Sprite {
    public function Main(  ) {
      var urlLoader:URLLoader = new URLLoader(  );
      urlLoader.addEventListener(Event.ruPLETE, completeListener);
      urlLoader.load(new URLRequest("someFile.txt"));
    }
    private function completeListener (e:Event):void {
      trace("Load complete");
    }
  }
}



Listen to the URLLoader complete event

 
package {
  import flash.events.*;
  import flash.net.*;
  import flash.display.*;
  public class Main extends Sprite{
    public function Main(  ) {
      var loader:URLLoader = new URLLoader(  );
      loader.addEventListener( Event.ruPLETE, handleComplete );
    //  loader.dataFormat = DataFormat.VARIABLES;
      loader.load( new URLRequest( "test.cfm" ) );
    }
    private function handleComplete( event:Event ):void {
      var loader:URLLoader = URLLoader( event.target );
  
      trace( "someText = " + loader.data.someText );
    }
  }
}



Load an XML file

 
package
{
    import flash.display.Sprite;
    import flash.events.*;
    import flash.net.*;
    public class NetworkProject extends Sprite
    {
        private var loader:URLLoader= new URLLoader(new URLRequest("http://example.ru/test.xml"));
    
        public function NetworkProject()
        {
            loader.addEventListener(Event.ACTIVATE, activatedListener);
            loader.addEventListener(Event.ruPLETE, completeListener);
            loader.addEventListener(ProgressEvent.PROGRESS, progressListener);
        }
    
        private function activatedListener(event:Event):void
        {
            trace(" activated " + loader.bytesLoaded + 
                  " but nothing loaded yet ");
        }
    
        private function completeListener(event:Event):void
        {
            trace(" all done loading " + loader.data + 
                  " and here"s the xml file we loaded ");
        }
    
        private function progressListener(event:Event):void
        {
            trace(" we"re in progress, we"ve loaded " + loader.bytesLoaded + 
                  " out of " + loader.bytesTotal + " bytes ");
        }
    }
}



Load a picture

 
package
{
    import flash.display.Sprite;
    import flash.display.Loader;
    import flash.net.URLRequest;
    import flash.events.Event;
    public class Main extends Sprite
    {
        private var loader:Loader;
    
        public function Main()
        {
            loader = new Loader();
            loader.load(new URLRequest("image.jpg "));
            loader.contentLoaderInfo.addEventListener(Event.ruPLETE,  
                contentComplete);
            addChild(loader);
        }
    
        private function contentComplete(event:Event):void
        {
            trace(loader.content);
        }
    }
}



Loading a Block of Text (Including HTML and XML)

 
package {
  import flash.display.*;
  import flash.text.*;
  import flash.events.*
  import flash.net.*;
  
  public class Main extends Sprite {
    private var _output:TextField = new TextField( );
    
    public function Main(  ) {
      _output.width = stage.stageWidth;
      _output.height = stage.stageHeight;
      
      addChild( _output );    
      
      var loader:URLLoader = new URLLoader(  );
      
//      loader.dataFormat = DataFormat.TEXT;
      
      loader.addEventListener( Event.ruPLETE, handleComplete );
      
      loader.load( new URLRequest( "example.html" ) );
    }
    
    private function handleComplete( event:Event ):void {
      var loader:URLLoader = URLLoader( event.target );
      
      _output.htmlText = loader.data;
    }
  }
}



Loading Images

 
package {
    import flash.display.Sprite;
    import flash.utils.ByteArray;
    import flash.display.Loader;
    import flash.net.URLLoader;
    import flash.net.URLRequest;
    import flash.net.URLLoaderDataFormat;
    import flash.geom.Rectangle;
    import flash.events.Event;
    public class Main extends Sprite {
        private var urlLoader:URLLoader = new URLLoader();
        public function Main() {
            urlLoader.dataFormat = URLLoaderDataFormat.BINARY;
            urlLoader.load(new URLRequest("http://www.wbex.ru/image2.jpg"));
           urlLoader.addEventListener(Event.ruPLETE, completeHandler);
       }
       private function completeHandler(event:Event):void {
           var loader:Loader = new Loader();
           loader.loadBytes(urlLoader.data);
           addChild(loader);
       }
    }
}



Loading XML

 
package {
  import flash.display.*;
  import flash.events.*;
  import flash.net.*;
  import flash.utils.*;
  public class Main extends Sprite {
    
    public function Main(  ) {
      var loader:URLLoader = new URLLoader(  );
      //loader.dataFormat = DataFormat.TEXT;
      loader.addEventListener( Event.ruPLETE, handleComplete );
      loader.load( new URLRequest( "example.xml" ) );
    }
    
    private function handleComplete( event:Event ):void {
      try {
        var example:XML = new XML( event.target.data );
        trace( example );
        
      } catch ( e:TypeError ) {
        trace( "Could not parse text into XML" );
        trace( e.message );
      }
    }
  }
}



Trace loader data

 
package {
  import flash.events.*;
  import flash.net.*;
  import flash.display.*;
  public class Main extends Sprite{
    public function Main(  ) {
      var loader:URLLoader = new URLLoader(  );
      loader.addEventListener( IOErrorEvent.IO_ERROR, handleIOError );
      loader.addEventListener( HTTPStatusEvent.HTTP_STATUS, handleHttpStatus );
      loader.addEventListener( SecurityErrorEvent.SECURITY_ERROR, handleSecurityError );
      loader.addEventListener( Event.ruPLETE, handleComplete );
    //  loader.dataFormat = DataFormat.VARIABLES;
      loader.load( new URLRequest( "example.txt" ) );
    }
    function handleIOError( event:IOErrorEvent ):void {
      trace( "Load failed: IO error: " + event.text ); 
    }
    function handleHttpStatus( event:HTTPStatusEvent ):void {
      trace( "Load failed: HTTP Status = " + event.status );
    }
    function handleSecurityError( event:SecurityErrorEvent ):void {
      trace( "Load failed: Security Error: " + event.text );
    }
    function handleComplete( event:Event ):void {
      var loader:URLLoader = URLLoader( event.target );
      
      for ( var property:String in loader.data ) {
        trace( property + " = " + loader.data[property] );
      }
    }
}
}



Understanding Flash Player Security

 
PHPCommunication
package
{
    import flash.display.Sprite;
    import flash.net.URLLoader;
    import flash.net.URLRequest;
    import flash.events.Event;
    public class Main extends Sprite
    {
        private var _username:String = "josh";
        private var _id:String = "143";
        private var loader:URLLoader;
    
        public function Main()
        {
            loader = new URLLoader();
            loader.addEventListener(Event.ruPLETE, dataLoaded);
            var request:URLRequest = new URLRequest("http://localhost:8888/PHPScript.php?username=" + _username + "&id=" + _id);
            loader.load(request);
        }
    
        public function dataLoaded(event:Event):void
        {
            var xmlResponse:XML = XML((event.target as URLLoader).data);
            trace(xmlResponse);
        }
    }
}



URLLoader Complete event

 
package {
  import flash.display.*;
  import flash.net.*;
  import flash.events.*;
  public class Main extends Sprite {
    public function Main(  ) {
      var urlLoader:URLLoader = new URLLoader(  );
      urlLoader.addEventListener(Event.ruPLETE, completeListener);
      urlLoader.load(new URLRequest("someFile.txt"));
    }
    private function completeListener (e:Event):void {
      var loadedText:String = URLLoader(e.target).data;
    }
  }
}