Quantcast
Viewing latest article 1
Browse Latest Browse All 2

JSON and AS3 URLRequest Class

A while ago, the as3corelib has been published on Google Code with a useful tool: JSON encoder and decoder.

JSON (JavaScript Object Notation) is a lightweight computer data interchange format (that is intended to solve XML structure weight), that allows you to send Object (Array, Strings, Number, …) to your server scripts or to JavaScript.

JSON is commonly used by Yahoo Web Services for example.

To use JSON and URLRequest even faster and simpler, I’ve create a small class that allows you to configure your URLRequest in one line (called PhpInteract), to add variables and to precise if your variables are encoded in JSON.

AS3 example to load a XHTML file:

var mi:PhpInteract = new PhpInteract(); //New object of the PhpInteract Class
mi.loadFile("http://www.w3.org/TR/2008/WD-xhtml-access-20080107/");

Now to get the content of the file just do as usual with the onComplete Listener:

mi.addEventListener(Event.COMPLETE, completeHandler);

public function completeHandler(e:Event):void
	{
		//file is completely loaded
		trace("File Content :n"+mi.content);
	}

You are certainly thinking that a new Class to do this is completely useless… And I agree. Now we can do something more interesting and pass variables (encoded in JSON) to our server side script:

var arr:Array = new Array("a", "b", "c");//An array for the demo...
mi.loadFile("http://yourserver/fromFlash.php",arr, true);
mi.isJSON = true; //We specify that the content must be JSON encoded

Now for the demo, our below PHP code will decode the received JSON string and will re-encode it in JSON and echo it. Our Flash / Flex / AIR application will be able to decode the content as well and handle complex data from PHP (or any server side languages that can encode and decode JSON).

< ?php
$flash = $_POST['fromFlash'];

$testArray = json_decode($flash);

//We can access to the Array Element
echo "Array [0]".$testArray[0]."n";
echo "Array [1]".$testArray[1]."n";
echo "Array [2]".$testArray[2]."n";

echo json_encode($testArray);
?>

What you need to make it work:
The as3corelib
The PhpInteract Class

Below is a complete sample:

package
{
	import flash.display.Sprite;
	import ch.metah.mURL.PhpInteract;
	import flash.events.*;

	public class InitmURL extends Sprite
	{
		var mi:PhpInteract = new PhpInteract();
		public function InitmURL()
		{
			//var arr:Array = new Array("a", "b", "c");//for the demo
			//mi.loadFile("http://metah.ch/seo/fromFlash.php",arr, true);
			//mi.isJSON = true; //is JSON content in loaded file?

			mi.loadFile("http://www.w3.org/TR/2008/WD-xhtml-access-20080107/");

			//USE EventListener
			mi.addEventListener(Event.COMPLETE, completeHandler);
			mi.addEventListener(Event.OPEN, openHandler);
			mi.addEventListener(ProgressEvent.PROGRESS, progressHandler);
			mi.addEventListener(HTTPStatusEvent.HTTP_STATUS, HTTPHandler);
			mi.addEventListener(SecurityErrorEvent.SECURITY_ERROR, errorHandler);
		}

		public function completeHandler(e:Event):void
		{
			//file is completely loaded
			trace("File Content :n"+mi.content);
		}
		public function openHandler(e:Event):void
		{
			//trace("Download started");
		}
		public function progressHandler(e:ProgressEvent):void
		{
			//Handle Progress
			//trace("Loaded"+mi.bytesLoaded+"Total"+mi.totalBytes);
		}
		public function HTTPHandler(e:HTTPStatusEvent):void
		{
			//trace("HTTP Status:"+mi.httpStatus);
		}
		public function errorHandler(e:SecurityErrorEvent):void
		{
			//Handle Security Error Here
		}

	}
}

Ahmet


Viewing latest article 1
Browse Latest Browse All 2

Trending Articles