Sunday, March 28, 2010

URLLoader does not work

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 += '' %26gt; '' + Math.round(elapsedTime/100)/10 + '' %26lt; '';

?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,downloadSecurityE rrorHandler);
?dispatcher.addEventListener(HTTPStatusEvent.HTTP_STATUS,downloadHttpStatusHandl er);
?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 + '')'';
?}
?}
}

URLLoader does not work

notice that the url in the posted sampe says http://test.txt but as i explained above i tried ''test.txt'' file://test.txt'' before trying this last one.

URLLoader does not work

silly mistake... notice how all callback functions take an argument ''event''Event'' ... all that is _except_ the downloadCompleted handler

i changed

private function downloadCompletedHandler():void

to

private function downloadCompletedHandler(event:Event):void

and this code started to work

unfortunatly, the compiler does not catch this sort of error.

This question has been answered

No comments:

Post a Comment