FlashDevelop receives XML, Flash Pro receives JSON - json

I'm getting a data feed from a client's supplier.
Documention says that I can get the feed both in XML and in JSON, but that the default is XML. To get JSON I have to set a header Accept:application/json and remove Accept:application/xml header.
I have not set nor removed any headers in my code.
I'm using a normal URLLoader to load the feed.
When I navigate to the feed url in a browser, I get XML.
When I run my code in FlashDevelop, I get XML.
When I run the exact same code in Flash Pro, I get JSON.
Does anyone have any idea what is causing this in Flash Pro? Any hidden setting that can be changed?
URLRequest.method has no impact. Debug mode or not has no impact.
I'm stomped.
---------code I tried------------
var header:URLRequestHeader = new URLRequestHeader("Accept", "application/xml");
var headers:Array = [];
headers.push(header);
urlReq.requestHeaders = headers;
urlReq.method = URLRequestMethod.POST;
--------- full load code -----------
_urlLoader.addEventListener(Event.COMPLETE, validateFeedLoaded);
_urlLoader.addEventListener(IOErrorEvent.IO_ERROR, handleFeedIOerror);
_urlLoader.addEventListener(SecurityErrorEvent.SECURITY_ERROR, handleFeedSecError);
var urlReq:URLRequest = new URLRequest(_feedUrl);
urlReq.requestHeaders = [new URLRequestHeader("Accept", "application/json")];
_urlLoader.load(urlReq);

Different clients/applications have different defaults. Different versions of the flash/air runtime may have different defaults for the accept type.
If you need consistency, then you should explicitly set the accept type in your request:
urlrequest.requestHeaders = [new URLRequestHeader("Accept", "application/json")];
This should ensure you get JSON back in both IDE's in whatever version of the runtime you're using.

Related

Flash sandbox not enabling me getting JSON data from server - AS3

i'm setting up a local SWF file that has to display some JSON data retrieved from a remote web server in some dynamic text fields.
Code:
Security.allowDomain("api.yourserver.com");
var loader:URLLoader = new URLLoader();
var request:URLRequest = new URLRequest("http://api.yourserver.com/yourendpoint");
request.method = URLRequestMethod.GET;
request.contentType = "application/json";
loader.load(request);
loader.addEventListener(Event.COMPLETE, decodeJSON);
function decodeJSON(event:Event):void{
var loader:URLLoader = URLLoader(event.target);
var Info:Object = JSON.parse(loader.data);
cont.textfield1.text = Info.text.field1;
cont.textfield2.text = Info.text.field2;
cont.textfield3.text = Info.text.field3;
}
Control > Test - It works. When run standalone it doesn't. I get the 2028 error (sandbox violation).
What i tried:
The LoaderContext method explained here on StackOverflow but i get a 2124 error (Loaded file is an unknown type - seems like the Loader method can only be used with stuff like SWF or medias like JPG etc.);
Setting local playback as described always here on StackOverflow but it didn't help;
Setting up and exception in the Global Flash Player Trust directory as explained here but got the 2028 again;
Anyone who was able to overcome this and willing to explain how or at least pointing in the right direction?
Thanks in advance!
I think that your current published file has just assess to local files only and not network ones, that's why you got security #2028 error.
To avoid that, you can change the Local playback security for your published swf from the Publish Settings to Access network only :
If you still get security errors when testing locally ( like #2048 error ), take a look on my answer of this question to add your local swf file to the trusted locations ...
Hope that can help.

how to load and edit a sql database from adobe flex builder 4.6 air program

Alright I have my sqldb hosted online and can access it using phpmyadmin what i would like to do is create tables and add items to the tables via adobe flex builder 4.6 desktop AIR application.
Anyone know if i am able to do this, the idea for the program is so person at position A can enter a name and person at position B can then use his program to read those names
According to Accessing mysql from Adobe flex/AIR, AIR is unable to access MySQL servers directly, so you'll have to use web services or some custom API do to this. But yeah, sure it's possible to do what you want.
I agree with Isaac. However, I want to add that it is good coding practices to not allow client side applications modify a database directly. In the applications that I have built, I like to use PHP to set-up an API that then interacts with the database. The AIR application then the interaction with the API using HTTP Requests.
Following code shows how to perform a URL request from the Adobe Website.
http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/net/URLRequest.html
var url:String = url location of the API;
var request:URLRequest = new URLRequest(url);
var variables:URLVariables = new URLVariables(); //create variables to pass to the API
variables.exampleSessionId = new Date().getTime(); //create variables to pass to the API
variables.exampleUserLabel = "guest"; //create variables to pass to the API
request.data = variables; //Add the variables to the request
request.method = URLRequestMethod.POST; //Set the method of the Request GET, POST, PUT
navigateToURL(request); //Executes the request
You'd probably like something like https://backendless.com/ for this.

Flex mobile HTTPMultiService set user-agent

I'm developing a Flex mobile application and I'm using the Http Services within FlashBuilder (4.7) to send/receive data. I'm having some issues with how the server is setup to accept calls (from the mobile platform) and apparently setting the User-Agent in Android works just fine. But I can't seem to be able to find a way to set the User-Agent in FlashBuilder.
Any ideas?
Thanks
var request:URLRequest=new URLRequest('URL_OF_YOUR_SERVICE');
var userCredential:Object = new Object();
userCredential['user_email'] = 'YOUR_MAIL';
userCredential['user_password'] = 'YOUR_PASSWORD';
request.data=JSON.stringify(userCredential);// use this if your service accept json only else give Object directly
request.method = 'post';// specify here method GET or POST
var loader:URLLoader=new URLLoader();
loader.addEventListener(Event.COMPLETE, userLoginCompleteHandler);
loader.addEventListener(IOErrorEvent.IO_ERROR, onIOErrorHanlder);
try
{
loader.load(request);
}
catch(error:Error)
{
trace("Login Error:: "+ error.toString());
}
may this will help you, here i have defined userLoginCompleteHandler and onIOErrorHanlder event listner for result and fault.
No, you can't set the User-Agent header in Flex.
From this Adobe forums post:
Note again that since the underlying Flash API is flash.net.URLLoader there are certain headers that
you cannot send. For details, please see the docs for flash.net.URLRequestHeader.
The URLRequestHeader docs say:
In Flash Player and in Adobe AIR content outside of the application
security sandbox, the following request headers cannot be used:
... User-Agent, ...

AS3 URLRequest POST Only Works When Compiled in HTML Wrapper

I'm attempting to send a POST request with data to another domain with this code:
_snapshot_id = 1369022400;
var urlRequest:URLRequest = new URLRequest("https://fuzzykittens/radar");
urlRequest.method = URLRequestMethod.POST;
urlRequest.contentType = "application/x-www-form-urlencoded";
//set variables for post
var postVars:URLVariables = new URLVariables();
postVars.snapshot = String(_snapshot_id);
urlRequest.data = postVars;
//initialize weather proccess request
weatherProcRequest = new URLLoader(urlRequest);
weatherProcRequest.addEventListener(Event.COMPLETE,
weatherProcRequest_CompleteHandler);
weatherProcRequest.addEventListener(IOErrorEvent.IO_ERROR,
weatherProcRequest_ErrorHandler);
weatherProcRequest.addEventListener(SecurityErrorEvent.SECURITY_ERROR,
weatherProcRequest_ErrorHandler);
weatherProcRequest.load(urlRequest);
When I set the flex compiler to use an HTML wrapper, the request works. When I don't use a wrapper, the request throws an io error #2032. I think it's not sending the snapshot id, but I don't know why.
Is there any obvious reason why a request would send data when debugged in an html wrapper and fail to do so when debugged outside an html wrapper?
fuzzykittens has a crossdomain.xml with
<allow-access-from domain="*" secure="false"/>
I found the issue. Adobe very briefly mentions this caveat in URLRequest's livedoc:
Note: If running in Flash Player and the referenced form has no body, Flash Player automatically uses a GET operation, even if the method is set to URLRequestMethod.POST. For this reason, it is recommended to always include a "dummy" body to ensure that the correct method is used.
When I just ran the SWF without a body, the snapshot id was not sent as a post var so the server responded differently.
It's definitely not the answer I wanted, but it feels good to know what was going on. Thank you for your comments.

POST file upload using URLRequest

I have a quick question regarding POST file uploads in ActionScript 3. I am trying to upload a ByteArray from memory via POST to a server. I'm using the URLRequest class to send the data, and URLLoader because I want to monitor the progress. The relevant sections of code follows:
var uploadRequest:URLRequest = new URLRequest("http://127.0.0.1/upload.php");
uploadRequest.method = URLRequestMethod.POST;
uploadRequest.contentType = "multipart/form-data";
uploadRequest.data = myByteArray;
var uploader:URLLoader = new URLLoader;
uploader.addEventListener(ProgressEvent.PROGRESS, onUploadProgress);
uploader.addEventListener(Event.COMPLETE, onUploadComplete);
uploader.dataFormat = URLLoaderDataFormat.BINARY;
uploader.load(uploadRequest);
The problem is that I've set my callbacks to trace the upload progress, and the bytesTotal property of the ProgressEvent is always 1960 (the size of the request minus data?), even though the actual data is around 20MB and no file is uploaded even after the Complete event fires.
I've verified that upload.php functions correctly with a simple html form, and I can verify that myByteArray contains all of the data in question. Can anyone tell me what I'm doing wrong?
Edit:
I've attempted a couple of new things that I thought I should mention. The first is setting the content type to application/octet-stream instead of multipart/form-data, which had no effect other than increasing the number of bytes to 1964. I also checked the Apache error log and found the following text repeated a lot:
PHP Warning: Missing boundary in multipart/form-data POST data in Unknown on line 0
I'm guessing that Flash isn't formatting the HTTP request properly for whatever reason. Given that I created a FileReference that makes use of the same methods I set for the URLLoader to upload a file from disk, and got the expected result: the bytesTotal property matched the file size and the file was uploaded correctly.
Working from that I found a page in the Adobe developer docs that mentions uploading data to a server using FileReference.upload() by setting the data parameter of the URLRequest, so I tried the following code:
var uploadRequest:URLRequest = new URLRequest("http://127.0.0.1/upload.php");
uploadRequest.method = URLRequestMethod.POST;
uploadRequest.data = myByteArray;
fileRef = new FileReference;
fileRef.addEventListener(ProgressEvent.PROGRESS, onUploadProgress);
fileRef.addEventListener(Event.COMPLETE, onUploadComplete);
fileRef.upload(uploadRequest);
Which resulted in the following output:
ArgumentError: Error #2127: FileReference POST data cannot be type ByteArray.
I'm really stuck here. Any suggestions would be appreciated!
You should add more info to the "Content-Type" header:
uploadRequest.contentType = "multipart/form-data; boundary=<<boundary here>>";