Using the URLLoader and URLVariables classes
|
|
|
|
| Articles Reviews Actionscript | |
| Written by Bogdan V | |
| Saturday, 03 February 2007 | |
|
ActionScript 3.0 has replaced the LoadVars class with URLLoader and URLVariables classes. The URLLoader class downloads data from a URL as text, binary data, or URL-encoded variables. The URLLoader class is useful for downloading text files, XML, or other information to use in dynamic, data-driven ActionScript applications. The URLLoader class takes advantage of the ActionScript 3.0 advanced event handling model, which allows you to listen for such events as complete, httpStatus, ioError, open, progress, and securityError. The new event handling model is a significant improvement over the ActionScript 2.0 support for the LoadVars.onData, LoadVars.onHTTPStatus, and LoadVars.onLoad event handlers because it allows you to handle errors and events more efficiently. Much like the XML and LoadVars classes in earlier versions of ActionScript, the data of the URLLoader URL is not available until the download has completed. You can monitor the progress of the download (bytes loaded and bytes total) by listening for the flash.events. ProgressEvent.PROGRESS event to be dispatched, although if a file loads too quickly a ProgressEvent.PROGRESS event may not be dispatched. When a file has successfully downloaded, the flash.events.Event.COMPLETE event will be dispatched. The loaded data is decoded from UTF-8 or UTF-16 encoding into a string. The URLLoader.load() method (and optionally the URLLoader class’s constructor) takes a single parameter, request, which is a URLRequest class object. A URLRequest object contains all of the information for a single HTTP request, such as the target URL, request method (GET or POST), additional header information, and the MIME type (for example, when you upload XML content). For example, to upload an XML packet to a server-side script, you could use the following ActionScript 3.0 code: var secondsUTC:Number = new Date().time; var dataXML:XML = <login><time>{secondsUTC}</time><username>Ernie</ The previous snippet creates an XML instance named dataXml that contains an XML packet to be sent to the server. Next, you set the URLRequest contentType property to "text/xml" and set the URLRequest data property to the contents of the XML packet, which are converted to a string by using the XML.toXMLString() method. Finally, you create a new URLLoader instance and send the request to the remote script by using the URLLoader.load() method. There are three ways in which you can specify parameters to pass in a URL request:
When you define variables within the URLVariables constructor or within the URLVariables.decode() method, you need to make sure that you URL-encode the ampersand (&) character because it has a special meaning and acts as a delimiter. For example, when you pass an ampersand, you need to URL-encode the ampersand by changing it from & to %26 because the ampersand acts as a delimiter for parameters. Powered by jReviews |
|
| Last Updated ( Thursday, 03 January 2008 ) | |
| < Prev | Next > |
|---|







