as3 parse a string into URLVariables - actionscript-3

for instance if a string is
data = "success=0"
or
data = "success=1&registration=20";
how to parse all variables into URLVariables class object.

var urlVars:URLVariables = new URLVariables(data);
or
urlVars.decode(data);

If i remember right this will solve your problem.
var urlvar:URLVariables = new URLVariables(data);

Related

create a JSON file and save into disk

how to create a JSON file for any object and save it on disk. after some days retrieve it back?
To write to a shared object
var so:SharedObject = SharedObject.getLocal("mySharedObject");
so.data.storedJSON = myJSON;
so.flush();
To retrieve it back elsewhere
var so:SharedObject = SharedObject.getLocal("mySharedObject");
myJSON = so.data.storedJSON;
convert to ByteArray
save
read a this documentation: registerClassAlias
registerClassAlias("com.myDomain", MyClass);
var myClass:MyClass = new MyClass();
var ba:ByteArray = new ByteArray();
ba.writeObject(myClass);
so.data.byteArray = ba;
ba.position = 0;
read
myClass = so.data.byteArray.readObject();
Use SharedObjects. Read up on them here.

Reading MySQL result using PHP in AIR

I was wondering if some one could explain how I can read a MySQL result array in AS3.
I am using:
var loader:URLLoader = new URLLoader();
var urlReq:URLRequest = new URLRequest("http://domain.com/get-api.php");
var urlVars:URLVariables = new URLVariables();
loader.dataFormat = flash.net.URLLoaderDataFormat.TEXT;
urlReq.method = URLRequestMethod.POST;
urlReq.data = urlVars;
urlVars.mySubmittedRateID = GlobalVariables.mySubmittedRateID;
urlVars.myPostcode = GlobalVariables.currentPostcode;
loader.addEventListener(flash.events.Event.COMPLETE, getDataOnComplete);
loader.load(urlReq);
to pass a parameter (2 in fact) to the get-api.php file. In the file I PHP reading in the parameters and a normal SQL select where statement which gets all the results matching the parameters.
My getDataOnComplete function is:
private function getDataOnComplete(event:flash.events.Event):void
{
var loader:URLLoader = new URLLoader(event.target);
var resultsArray:Array = JSON.decode(loader.data);
}
My PHP:
$row_array['done'] = "true";
return json_encode($row_array);
Error:
Error: Error #2101: The String passed to URLVariables.decode() must be a URL-encoded query string containing name/value pairs.
at Error$/throwError()
at flash.net::URLVariables/decode()
at flash.net::URLVariables()
at flash.events::EventDispatcher/dispatchEventFunction()
at flash.events::EventDispatcher/dispatchEvent()
at flash.net::URLLoader/onComplete()
For some reason I cant even get to the getDataOnComplete function.
Your php mysql result will be array then
In php side you need to convert into result array into json_encode($result_array) like
echo json_encode($result_array); //Note here not return statement;
In as3 side
you can use latest AIR SDK having native JSON class (Here using AIR class) or as3corelib to parse JSON data.
private function getDataOnComplete(event:flash.events.Event):void
{
var resultObj:Object = event.target.data;
var resultsArray:Object = JSON.stringify(resultObj);
}
For more details pass-an-array-from-php-to-actionscript-3

Actionscript 3 Flex 4.5 Serialization/Deserialization issue?

I'm having a particular issue deserializing an object in a Flex 4.5 Mobile project. I've connected to a Webservice fine and populate a ListBox fine. I can select the item and get the details just fine as well but after serializing the object and trying to deserialize it; the Object definition is getting lost somewhere.
I have a variable for when the user selected the request in a List
private var selectedReq:ServiceRequest;
//Here we instantiate the local variable when user select id in ListBox
selectedReq = event.currentTarget.selectedItem as ServiceRequest;
Each Service Request the user chooses to save will call this method.
private function writeServiceRequest():void {
var filename:String = buildFileName();
var file:File = File.applicationStorageDirectory.resolvePath(filename);
if (file.exists)
file.deleteFile();
var fileStream:FileStream = new FileStream();
fileStream.open(file, FileMode.WRITE);
//selectedReq is the private var of the users selected item
fileStream.writeObject(selectedReq);
fileStream.close();
}
When the users want to view the request this method is called.
private function readServiceRequest():ServiceRequest {
var filename:String = buildFileName();
var file:File = File.applicationStorageDirectory.resolvePath(filename);
if (!file.exists) {
return null;
}
var fileStream:FileStream = new FileStream();
fileStream.open(file, FileMode.READ);
var objReq:ServiceRequest = fileStream.readObject() as ServiceRequest;
fileStream.close();
return objReq;
}
The Class Object is similar to.
public var id:uint;
public var requisitioner:String;
public var requestItems:ArrayCollection //Webservice it's actually List<requestItems>
public var requestProcesses:ArrayCollection // WSDL it's actually List<>
When I try to read/deserialize like
//This line is null but the file exist and the object was written
var objReq:ServiceRequest = readServiceRequest() as ServiceRequest;
if(objReq) {
selectedReq = objReq;
}
If I do not cast the readServiceRequest() as ServiceRequest and simply return an Object; I can iterate through the Object and get the correct values returned from the serialized object.
I can only assume the Classes that Flex created from the Webservice may be causing this? If the values are getting written but not the object type then something has to be lost in the serialization - correct?
Sorry for all the details but I'm a little lost at this time.....any help would be appreciated.
RL
var objReq:ServiceRequest = readServiceRequest() as ServiceRequest;
The above line will continue to return null.
I bet that if you modify it in the following way:
var objReq:ServiceRequest = ServiceRequest(readServiceRequest());
You'll get a run-time exception with a message similar to Can't cast ObjectProxy to ServiceRequest
If that's the case, then the reason you get this is because the AMF serializer doesn't preserve the type information when serializing the ServiceRequest-instance.
In order to fix this you need to call flash.net.registerClassAlias() before the serialization/deserialization.
flash.net.registerClassAlias("fully.qualified.name.ServiceRequest", ServiceRequest);
Try if this work.
Check if event.result is ByteArray.
Read/store the event.result as ByteArray.
var byteArray:ByteArray = event.result as ByteArray;
Get/Deserialize Object using readObject() function.
var object:Object = byteArray.readObject();
Cast to targetted object type. ServiceRequest in your case.
var tartgettedObject:ServiceRequest = object as ServiceRequest;
or
var tartgettedObject:ServiceRequest = ServiceRequest(object);
My bad for not signing in before posting the question.
I did use registerClassAlias which did not help. I did find on the Adobe forum other simular issues and the constant feedback was the problem is most likely caused by the Object Class loosing it's attributes during the file write reason being the Class Objects are written when the Datasource is created from the Webservice. Since my original question we decided to use SQLite which is working perfectly fine - thanks for all you help.
RL

How do you combine two bytearrays in AS3?

I'm trying to combine two ByteArrays to send it out as post data using URLRequest.. Whenever I try to simply add them up, the request becomes GET instead of POST and the data for some reason doesn't get included.
create a total ByteArray by adding other ByteArray objects to it via the writeBytes() public method of the ByteArray class.
more info here: Reading and writing a ByteArray
Combining / Concatination Two Byte Arrays
Var Data:ByteArray = new ByteArray();
Var Salt:ByteArray = new ByteArray();
var DataAndSalt:ByteArray = new ByteArray();
DataAndSalt.length = (Data.length + Salt.length);//Defines the **length of Resultant Array**
//Array Copy Method(VB)/ Concate the ByteArray(ActionScript) one After another
DataAndSalt.writeBytes(Data);
DataAndSalt.writeBytes(Salt);
I will Show here Conversion of String into Byte Array and Merging Them (concate /combining) them into single byte array
// In Detail
var HashOut:ByteArray = new ByteArray();
var byterrData:ByteArray = new ByteArray();
var byterrSalt:ByteArray = new ByteArray();
//conversion of string Data and Salt in respective (UTF-8 and Default) Byte Array
var Data:String = "password";
var Salt:String ="‰ô©³¶í"; //Using Special Characters in a String variable
byterrData.writeMultiByte(Data, "iso-8859-1");
byterrSalt.writeMultiByte(Salt,Salt);
var DataAndSalt:ByteArray = new ByteArray();
DataAndSalt.length = (Data.length + Salt.length);
// Concate the ByteArray
DataAndSalt.writeBytes(Data);
DataAndSalt.writeBytes(Salt);
//Now You can Trace It by using
trace(DataAndSalt[0]);
trace(DataAndSalt[index Number]);
Not sure what your code looks like... the GET/POST issue is very weird.
However, use the below instead of trying to "add them up" (whatever that means).
array3 = array1.concat(array2);

Dynamically Instancing Objects ActionScript 3.0

I have a variable named "type". And I want to instance an object with the name of the value of type. Here is an example:
var myObjectName = "ball";
var object = new ball(); //Except I want to use the value of myObjectName.
I believe this used to be easy with AS2 when using _global, but I'm not sure how to do it in AS3?
Any help?
First get the class object with flash.utils.getDefinitionByName(), then instantiate that object:
var myClass:Class = getDefinitionByName(myObjectName) as Class;
var object:Object = new myClass();
http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/utils/package.html#getDefinitionByName()