Flash CS3: communicating with a Web Server

Friday December 14, 2007

ActionScript 3.0 introduces a number of formidable means of communication, some totally new, others revised and improved from the previous version of ActionScript. We begin to see changes as a way to interact with a Web Server in the case of a simple communication between Flash and a page with PHP XML response. First of all, the object XML no longer exists! In ActionScript 3.0 have been introduced new classes, some of which are dedicated to the "backwards".

Note: XML classroom (along with related classes) ActionScript 2.0 XmlDocument was renamed and moved to the package flash.xml. It is included in ActionScript 3.0 to ensure compatibility with older versions.

In ActionScript 2.0 communicating with a Web Server was often resolved using the coupled objects LoadVars and XML, using the method sendAndLoad() LoadVars (See: Flash: LoadVars and XML to send data POST). Now things are changing, but only just get used to new names and objects. As outlined in the manual, in fact:

The XmlDocument class represents the XML object that was preceding this in ActionScript 2.0. The name was changed to XmlDocument in ActionScript 3.0 to avoid conflicts with the name of the new class of XML ActionScript 3.0. In ActionScript 3.0, use the new class XML and related classes, that support E4X (ECMAScript for XML).

The XmlDocument class, as XmlNode and XMLNodeType, is to ensure compatibility retroactive. The functionality of loading XML documents is now provided by URLLoader class.

ActionScript 3.0 improves things, even if at first glance the classes are more interested than before. In our particular case in objects that are going to use are: URLVariables URLRequest URLLoader and XML.

Here is an example of sending and receiving:

Actionscript:
  1. URLVariables ( ) ; var variables: URLVariables = new URLVariables ();
  2. ; variables. myVariation = "Hello";
  3. URLRequest ( ) ; var request: URLRequest = new URLRequest ();
  4. ; request. url = "http://www.miodominio.com/miapagina.php";
  5. ; request. method = URLRequestMethod. POST;
  6. request. data = variables;
  7. URLLoader ( ) ; var loader: URLLoader = new URLLoader ();
  8. ; loader. dataFormat = URLLoaderDataFormat. TEXT;
  9. Event. COMPLETE , completeHandler ) ; loader. addEventListener (Event. COMPLETE completeHandler);
  10. try (
  11. request ) ; loader. load (request);
  12. ( error : Error ) { ) Catch (error: Error) (
  13. "Errore nel caricamento dell' URL" ) ; trace ( "Error loading of 'URL');
  14. )
  15. event:Event ) : void { completeHandler function (event: Event): void (
  16. = XML ( event. target . data ) ; var x_xml: XML = XML (event. target. data);
  17. x_xml ) ; trace (x_xml);
  18. )

The party server (miapagina.php) obviously does not change. Compared to the example shown in Flash: LoadVars and XML to send data POST notice that the object LoadVars was used in ActionScript 2.0 is as a vehicle of variables such as subject POST is sending through his method sendAndLoad() In ActionScript 3.0, these two functions are performed by two separate objects: URLVariables as a vehicle of variables (POST or GET) and URLLoader to send. The separation, of course, improves the situation because it gives us two objects "dedicated" that, among other things, play numerous important (and innovative) additional tasks - as we will see in the future.

On this subject (very large in ActionScript 3.0) come back later looking at the big news on other items for the communication and connection with the Web Server.

Related Post

Leave a comment

TAG XHTML <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <a <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Use <pre> to enclose code