Simple ActionScripts 3 read JSON from PHP - actionscript-3

I'm trying to read JSON data from localhost PHP in ActionsScripts3, I found some code to do it but this code doesn't works.
PHP:
<?php
$arr = array ('DATA1'=>"111",'DATA2'=>"222");
header('Content-Type: application/json');
echo json_encode($arr);
?>
AS3:
import flash.events.*;
import flash.net.*;
var urlLoader:URLLoader=new URLLoader();
function ReadJsonPhp () :void
{
addEventListener(Event.COMPLETE,init);
}
function init(event:Event)
{
urlLoader.load(new URLRequest("http://localhost/asphp.php"));
urlLoader.addEventListener(Event.COMPLETE, urlLoaderCompleteHandler);
}
function urlLoaderCompleteHandler(e:Event):void
{
trace(e.target.data) ;
var arrayReceived:Object = JSON.parse(e.target.data);
}
ReadJsonPhp();
This code have 3 function if possible i like to use only 1 function.

You can't do it in one function just because it's an async operation. You make a request and then wait for some response. The AS3 code just wrong and doesn't make any sense. Here is a simple example:
private var loader:URLLoader;
private var request:URLRequest;
private function load():void
{
request = new URLRequest("http://localhost/asphp.php");
loader = new URLLoader()
loader.addEventListener(Event.COMPLETE, onComplete);
loader.addEventListener(IOErrorEvent.IO_ERROR, onError);
loader.addEventListener(SecurityErrorEvent.SECURITY_ERROR, onError);
loader.load(request);
}
private function onError(e:Event):void
{
// handle error
}
private function onComplete(e:Event)
{
trace(e.target.data);
// keep in mind that if the Json string is invalid here will be SyntaxError exception!
var json:Object = JSON.parse( e.target.data );
trace( "json.DATA1 = ", json.DATA1 );
trace( "json.DATA2 = ", json.DATA2 );
}

Related

Putting Urlloader in a Class

I have searched on google en different pages i found the problem but not the solution.
I have a class i made and that class is called WebServiceController:
public class WebServiceController
{
private var url:String;
private var post:Object=new Object();
private var loader:URLLoader = new URLLoader();
private var postVariable:String="";
private var getVariable:String="";
private var Geladen:Boolean=false;
public function WebServiceController()
{
}
public function postUrlData(u:String,p:Object):String
{
url=u;
post=p;
var urlReq:URLRequest = new URLRequest (url);
var i:int=0;
for(var key:String in post)
{
//trace(key +" = " + post[key]);
if(i==0)
{
// urlVars.key = post[key];
postVariable=postVariable+""+key+"="+post[key];
}
else
{
//urlVars.key = post[key];
postVariable=postVariable+"&"+key+"="+post[key];
}
i++;
}
//trace(postVariable);
var urlVars:URLVariables = new URLVariables(postVariable);
//trace(urlVars);
// Add the variables to the URLRequest
urlReq.data = urlVars;
urlReq.method = URLRequestMethod.POST;
// Add the URLRequest data to a new Loader
//loader.load(urlReq);
// Set a listener function to run when completed
loader.addEventListener(Event.COMPLETE, onLoaderComplete);
// Set the loader format to variables and post to the PHP
loader.dataFormat = URLLoaderDataFormat.VARIABLES;
loader.load(urlReq);
function onLoaderComplete(event:Event):void
{
return loader.data;
}
}
Now from my movieclip i call the next function but it says undefined:
var wb:WebServiceController = new WebServiceController();
trace(wb.postUrlData(url,post));
I dont know how to solve this. I tried different things but it keeps saying undefined.
The URLLoader.load call is executed asynchronously so the data has not been returned when you attempt to trace it out in the class that instantiates your WebServiceController class.
To access the data in the parent class, your best bet is probably to dispatch an event from the WebServiceController class when the data has loaded and catch it in the parent class.
WebServiceController:
public function postUrlData(u:String, p:Object):void
{
url=u;
post=p;
var urlReq:URLRequest = new URLRequest (url);
var i:int=0;
for(var key:String in post)
{
//trace(key +" = " + post[key]);
if(i==0)
{
// urlVars.key = post[key];
postVariable=postVariable+""+key+"="+post[key];
}
else
{
//urlVars.key = post[key];
postVariable=postVariable+"&"+key+"="+post[key];
}
i++;
}
//trace(postVariable);
var urlVars:URLVariables = new URLVariables(postVariable);
//trace(urlVars);
// Add the variables to the URLRequest
urlReq.data = urlVars;
urlReq.method = URLRequestMethod.POST;
// Add the URLRequest data to a new Loader
//loader.load(urlReq);
// Set a listener function to run when completed
loader.addEventListener(Event.COMPLETE, onLoaderComplete);
// Set the loader format to variables and post to the PHP
loader.dataFormat = URLLoaderDataFormat.VARIABLES;
loader.load(urlReq);
}
private function onLoaderComplete(event:Event):void
{
// We can pass the event on like this
dispatchEvent(event);
}
Parent class:
public function initWebServiceController():void
{
var wb:WebServiceController = new WebServiceController();
wb.addEventListener(Event.COMPLETE, onWebServiceControllerDataLoaded);
wb.postUrlData(url, post);
}
private function onWebServiceControllerDataLoaded(event:Event):void
{
// The event target is the URLLoader instance. We can
// access the loaded data via its data property
trace(URLLoader(event.target).data);
}

URLLoader loads multi files and the result order is the same as call load()

Since URLLoader is async, how to make sure the order of data from server side is the same as the loader.load() call? In other words, the data order in totalResults is the same order of url-related content?
Following is code snippet:
1.for each(var url in urls) {
loadData(url);
}
2.private function loadData(url:String):void {
var urlLoader:URLLoader = new URLLoader();
urlLoader.addEventListener(Event.COMPLETE, completeHandler);
var request:URLRequest = new URLRequest(url);
urlLoader.load(request);
}
3.private function completeHandler(event:Event):void {
var loader:URLLoader = URLLoader(event.target);
var result:Object = loader.data;
totalResults.push(result);// suppose totalResults is Array and a property in the class.
}
You can extend URLLoader class functionality.
dynamic class DynamicURLLoader extends URLLoader { }
Then store data ( in your case probably index ) in loader object, before requesting:
var urlLoader:DynamicURLLoader = new DynamicURLLoader();
urlLoader.index = ...
After response, retrieve that data ( in your case index ):
var loader:DynamicURLLoader = DynamicURLLoader(event.target);
totalResults[ loader.index ] = loader.data;
you could either use BulkLoader - it got this build-in. or you have to build your own queue, where one file is loaded after the other.
code snippet:
0.var queue:Array, totalResults:Array = [];
var urlLoader:URLLoader = new URLLoader();
urlLoader.addEventListener(Event.COMPLETE, completeHandler);
1.for each(var url in urls) {
loadData(url);
}
2.private function loadData(url:String):void {
var request:URLRequest = new URLRequest(url);
queue.push(request);
}
3.private function doQueue() {
if (queue.length > 0) {
var arr:Array = queue.splice(0,1);
var req:URLRequest = arr[0] as URLRequest;
urlLoader.load(req);
}
else {
// queue done !
trace(totalResults);
}
}
4.private function completeHandler(event:Event):void {
var loader:URLLoader = URLLoader(event.target);
var result:Object = loader.data;
totalResults.push(result);// suppose totalResults is Array and a property in the class.
doQueue();
}
You can load each url sequentially one after the other in order like in the following example:
package
{
import flash.display.Sprite;
import flash.events.Event;
import flash.net.URLLoader;
import flash.net.URLRequest;
public class Main extends Sprite
{
private var _urls:Vector.<String>;
private var _counter:int;
private var _data:Array;
public function Main():void
{
if (stage) init();
else addEventListener(Event.ADDED_TO_STAGE, init);
}// end function
private function init(e:Event = null):void
{
removeEventListener(Event.ADDED_TO_STAGE, init);
_urls = Vector.<String>(["text1.txt", "text2.txt", "text3.txt" ]);
_counter = 0;
_data = [];
loadNext();
}// end function
private function loadNext():void
{
var urlLoader:URLLoader = new URLLoader();
urlLoader.addEventListener(Event.COMPLETE, onComplete);
urlLoader.load(new URLRequest(_urls[_counter]));
}// end function
private function onComplete(event:Event):void
{
_data.push((event.target as URLLoader).data);
if (_counter++ == (_urls.length - 1)) trace("complete");
else loadNext();
}// end function
}// end class
}// end package
The methods loadNext() and onComplete() act as a loop. When loadNext() is called, an URLLoader object is instantiated and loads an url in the Vector.<String> object _urls. It uses the _counter object as a counter that is increment upon each UrlLoader object's "complete" event.
When the onComplete() event handler is called, it pushes the data loaded by the URLLoader object into an array called _data. Finally an if statement checks to see if all the urls have been loaded, if not, it increments the counter, if so, it executes the rest of the code in the application, in this case trace("complete");.
Why not just use HTTPService, which allows you to use AsyncToken to determine which initial call resulted in which initial result? That way, you don't have to wait between calls (with the performance degradation that implies.) Here is some documentation about how it works with Remoting, which is similar http://flexdiary.blogspot.com/2008/11/more-thoughts-on-remoting.html . With HttpService, you get back the token as a result of the send() method.

AS3 - I can't get a string value returned from a function

AS3 code, from a sample, I want to have the value in the string 'location' available to other parts of the main program. It returns fine in the completed handler, but how do I make it available to the first part?
package {
import flash.display.MovieClip;
import flash.display.MovieClip;
import flash.events.*
import flash.net.*;
import flash.net.URLVariables;
public class turl extends MovieClip {
public var location:String = new String();
public function turl() {
// constructor code
var variables:URLVariables = new URLVariables();
variables.url = String("xxxxxxxxx");
sendAndLoad("xxxxxxxx", variables)
// THIS TRACE WILL NOT DISPLAY THE LOCATION _ A TINY URL
trace("TinyURL: " + location);
}
function sendAndLoad(url:String, _vars:URLVariables ):void {
var request:URLRequest = new URLRequest(url);
var _urlloader:URLLoader = new URLLoader();
_urlloader.dataFormat = URLLoaderDataFormat.TEXT;
request.data = _vars;
request.method = URLRequestMethod.POST;
_urlloader.addEventListener(Event.COMPLETE, handleComplete);
_urlloader.addEventListener(IOErrorEvent.IO_ERROR, onIOError);
_urlloader.load(request);
}
function handleComplete(event:Event):void {
var loader:URLLoader = URLLoader(event.target);
location = loader.data;
trace("TinyURL: " + location);
}
function onIOError(event:IOErrorEvent):void {
trace("Error loading URL.");
}
}
}
package
{
import flash.display.MovieClip;
import flash.display.MovieClip;
import flash.events.*
import flash.net.*;
import flash.net.URLVariables;
public class turl extends MovieClip
{
public static var Location:String;
public function turl() {
// constructor code
var variables:URLVariables = new URLVariables();
variables.url = String("http://www.designscripting.com");
sendAndLoad("http://tinyurl.com/api-create.php", variables)
// THIS TRACE WILL NOT DISPLAY THE LOCATION _ A TINY URL
trace("TinyURL: " + Location);
}
function sendAndLoad(url:String, _vars:URLVariables ):void
{
var request:URLRequest = new URLRequest(url);
var _urlloader:URLLoader = new URLLoader();
_urlloader.dataFormat = URLLoaderDataFormat.TEXT;
request.data = _vars;
request.method = URLRequestMethod.POST;
_urlloader.addEventListener(Event.COMPLETE, handleComplete);
_urlloader.addEventListener(IOErrorEvent.IO_ERROR, onIOError);
_urlloader.load(request);
}
function handleComplete(event:Event):void {
var loader:URLLoader = URLLoader(event.target);
Location= loader.data;
trace("TinyURLss: " + Location);
}
function onIOError(event:IOErrorEvent):void {
trace("Error loading URL.");
}
}
}
static variable Location holds your String value and you can get this String value
anywhere in class and outside the class.
The trace statement in the constructor doesn't work because that trace happens immediately after the data request is made, before the data has been downloaded and location has been set. The constructer is meant for setting the initial conditions of an object. The only way to make the result of the data request immediately available to the constructor is to pass it in directly, but I think this would defeat the point of the class.
public function TURL(value:String)
{
location = value;
// Now this will work like you think.
trace("TinyURL: " + location);
}
I'm guessing you have other objects relying on this TURL class having a proper location. If that's the case, have the TURL class dispatch an event when it sets the location variable, indicating that it is ready to be used.
function handleComplete(event:Event):void
{
var loader:URLLoader = URLLoader(event.target);
location = loader.data;
dispatchEvent(new Event(Event.COMPLETE));
}
Tested and working!
var turl:Turl = new Turl("http://www.designscripting.com");
Once the URL has been received you can access it by trace(turl.loc);
package {
import flash.display.MovieClip;
import flash.events.*
import flash.net.*;
import flash.net.URLVariables;
public class Turl extends MovieClip {
public var loc:String;
public function Turl(urlToEncode:String):void {
var variables:URLVariables = new URLVariables();
variables.url = String(urlToEncode);
sendAndLoad("http://tinyurl.com/api-create.php", variables);
}
//2. send the request for the URL
private function sendAndLoad(url:String, _vars:URLVariables ):void {
var request:URLRequest = new URLRequest(url);
request.data = _vars;
request.method = URLRequestMethod.POST;
var _urlloader:URLLoader = new URLLoader(request);
_urlloader.dataFormat = URLLoaderDataFormat.TEXT;
_urlloader.addEventListener(Event.COMPLETE, handleComplete, false, 0, true);
_urlloader.addEventListener(IOErrorEvent.IO_ERROR, onIOError, false, 0, true);
_urlloader.load(request);
}
//3. handle the response. Only accessible once the response has been received.
private function handleComplete(event:Event):void {
event.target.removeEventListener(Event.COMPLETE, handleComplete);
event.target.removeEventListener(IOErrorEvent.IO_ERROR, onIOError);
loc = event.target.data;
trace("loc = "+event.target.data);
}
function onIOError(event:IOErrorEvent):void {
event.target.removeEventListener(Event.COMPLETE, handleComplete);
event.target.removeEventListener(IOErrorEvent.IO_ERROR, onIOError);
trace("Error loading URL.");
}
}
}

How to run function only when parameter variable has been assigned?

I need a way to wait running the parseCSV command until the readFile event has updated the content of importData. I have seen a few things about custom event dispatchers but cannot quite figure out how to use them in my situation.
private var importData : String;
public function importFile(event:MouseEvent):void {
var data:String = chooseFile();
parseCSV(importData);
}
public function chooseFile ():String {
var filetype:FileFilter = new FileFilter("CSV Files(*.csv)","*.csv");
var file:File = File.userDirectory;
file.browseForOpen("Select CSV file to import", [filetype]);
file.addEventListener(Event.SELECT, readFile);
return importData;
}
public function readFile (event:Event):void {
var filestream:FileStream = new FileStream();
filestream.open(event.target as File, FileMode.READ);
importData = filestream.readUTFBytes(filestream.bytesAvailable);
filestream.close();
}
You'll either need to add some callbacks or add some event listeners. I prefer callbacks:
function importFile(...) {
choseFile(function(file:File) {
readFile(file, parseCSV);
});
}
function choseFile(callback:Function) {
...
file.addEventListener(Event.SELECT, function(event:Event) {
callback(File(event.target));
});
}
function readFile(file:File, callback:Function) {
var data = ... read data from file ....;
callback(data);
}
What about just adding the line in the readFile function?
public function readFile (event:Event):void {
var filestream:FileStream = new FileStream();
filestream.open(event.target as File, FileMode.READ);
importData = filestream.readUTFBytes(filestream.bytesAvailable);
parseCSV(importData);
filestream.close();
}
The command will be executed as soon as importData is set.
If you wish to the custom events route, you need to dispatch your own custom Event. Each Event has a type parameter which is just a string to identify it with. For example Event.CHANGE is the same as using "change".
static public const CUSTOM = "myCustomEvent";
public function someConstructor():void {
addEventListener(CUSTOM, onCustomEvent);
}
public function testDispatch():void{
dispatchEvent(new Event(CUSTOM));
}
private function onCustomEvent(e:Event):void{
trace("custom event Dispatched");
}
So you could try something like this.
public function importFile(event:MouseEvent):void {
addEventListener(CUSTOM, onImport);
var data:String = chooseFile();
}
private function onImport(e:Event):void {
parseCSV(importData);
}
public function readFile (event:Event):void {
var filestream:FileStream = new FileStream();
filestream.open(event.target as File, FileMode.READ);
importData = filestream.readUTFBytes(filestream.bytesAvailable);
dispatchEvent(new Event(CUSTOM));
filestream.close();
}

Do you know if there's any problem with netstream.appendBytes() for streaming?

I use netstream.appendBytes to get the streaming (flv) from http, but works intermittently (it works, after refresh, doesn't work, then it works and so on...)
What is the problem?, I have not idea
My code is:
import flash.display.*;
import flash.events.*
import flash.net.*;
import flash.utils.ByteArray;
import com.hurlant.util.Hex;
var videoURL:String = "http://url/vivo/flash";
//elemento de conexíon
var conn:NetConnection = new NetConnection();
conn.connect(null);
//stream de red
var stream:NetStream;
//conexión
stream = new NetStream(conn);
//oyente
stream.addEventListener(AsyncErrorEvent.ASYNC_ERROR, asyncErrorHandler);
function Play()
{
var urlStream:URLStream = new URLStream();
//oyentes de URLStream
urlStream.addEventListener(StatusEvent.STATUS, inStatus);
urlStream.addEventListener(Event.COMPLETE, completeHandler);
urlStream.addEventListener(SecurityErrorEvent.SECURITY_ERROR, securityErrorHandler);
urlStream.addEventListener(ProgressEvent.PROGRESS, oyenteProcesoCarga);
//urlStream.addEventListener(ProgressEvent.PROGRESS, describeProcesoCarga);
urlStream.addEventListener(IOErrorEvent.IO_ERROR, ioError);
//Video
var video:Video = new Video(640,480);
video.attachNetStream(stream);
addChild(video);
stream.play(null);
urlStream.load(new URLRequest(videoURL));
}
function ioError(event:IOErrorEvent):void
{
textArea.text += event + "\n";
}
function oyenteProcesoCarga(event:ProgressEvent):void
{
var encr:ByteArray = new ByteArray();
event.target.readBytes(encr);
stream.appendBytes(encr);
}
function describeProcesoCarga(event:ProgressEvent):void
{
if (event.target.bytesAvailable > 0){
var encr:ByteArray = new ByteArray();
event.target.readBytes(encr);
}
}
function securityErrorHandler(event:SecurityErrorEvent):void {
}
function asyncErrorHandler(event:AsyncErrorEvent):void {
// ignore AsyncErrorEvent events.
}
function completeHandler(event:Event):void {
}
function inStatus(event:StatusEvent):void {
}
Play();
Maybe use a stream buffer as HTTP is TCP so not all packets arrive at time.