I am trying to POST to my firebase account, but I can't seem to figure out why it's not letting me. I can switch the POST to a GET and trace out the data I'm trying to get, but when I POST I get en error.
Code:
package
{
import flash.display.Loader;
import flash.events.Event;
import flash.events.IOErrorEvent;
import flash.net.URLLoader;
import flash.net.URLRequest;
import flash.net.URLRequestMethod;
import flash.net.URLVariables;
import flash.net.navigateToURL;
import flash.net.URLLoaderDataFormat;
//import flash.system.Security;
/**
* ...
* #author M
*/
public class FireBaseConnector
{
public function FireBaseConnector()
{
//Security.loadPolicyFile("https://demo.firebaseio-demo.com/crossdomain.xml");
trace("trying to post");
var url:String = 'https://[myURL].firebaseio.com/userData.json';
var req:URLRequest = new URLRequest(url);
var variables:URLVariables = new URLVariables();
variables.name = "alan"
req.data = variables;
req.method = URLRequestMethod.POST;
var l:URLLoader = new URLLoader()
//l.dataFormat = URLLoaderDataFormat.TEXT;
l.addEventListener(IOErrorEvent.IO_ERROR, onError);
l.addEventListener(Event.COMPLETE, handleResults);
l.load(req);
}
public function onError(e:IOErrorEvent):void {
trace("Error! " + e.text);
}
public function handleResults(e:Event):void {
trace("response" + e.target.data as String);
}
}
}
My output is:
[Starting debug session with FDB]
running
trying to post
Error! Error #2032: Stream Error. URL: https://resplendent-fire-2464.firebaseio.com/userData/.json
closing time!
closing time!
If i switch to GET my output is:
[Starting debug session with FDB]
running
trying to post
response{"dave":{"id":"678","name":"dave"},"mike":{"name":"mike"},"miles":{"id":"456","name":"miles"}}
closing time!
closing time!
That tells me I have the url right, but I have some permissions problem or something when trying to post. Maybe I have the variables sending improperly? I've made sure my Flash debugger is on the accepted app list in my Firewall.
I'm running Windows 10 and FlashDevelop JRE 1.6 Flex 4.6 Air 18
To send a POST request to the Firebase REST API you need to send the data JSON encoded:
var urlVars:Object = new Object();
urlVars.name = nameTxt.text;
urlVars.lastname = lastNameTxt.text;
var request:URLRequest = new URLRequest(FIREBASEURL);
request.data = JSON.stringify(urlVars);
request.method = URLRequestMethod.POST;
var firebaseLoader:URLLoader = new URLLoader();
firebaseLoader.addEventListener(flash.events.Event.COMPLETE, function():void
{
trace(firebaseLoader.data);
});
firebaseLoader.load(request);
I try this code can work
import flash.net.URLLoader;
import flash.net.URLRequest;
import flash.events.Event;
var urlVars:Object = new Object();
urlVars.name = "111111";
var url:URLLoader=new URLLoader();
var hdr:URLRequestHeader = new URLRequestHeader("Content-type", "application/json");
var req:URLRequest = new URLRequest( "https://[myURL].firebaseio.com/userData.json" );
req.method = URLRequestMethod.POST;
req.data = JSON.stringify(urlVars);
url.load(req);
url.addEventListener(Event.COMPLETE,urlCompelte)
function urlCompelte(e:Event){
trace(e.target.data);//{"name":"-Ka6PGZc_hY-sfJJqeVS"}
}
Related
I have an app which records and upload mic input encoding in mp3.
When I test locally, in Flash IDE, it works fine, my audio file is uploaded.
What I have tried:
All my files are on swf_dir (see in the code below)
My embed tag already have allowScriptAcess set to always;
Already put a generic crossdomain.xml in my host root and in swf_dir;
My main class already load a policy file and allow all domains (even this being unnecessary, because it's in the same server): system.security.loadPolicyFile("http://www.host.com/swf_dir/crossdomain.xml") and system.Security.allowDomain("*")
Here is the upload/encode class.
package
{
import flash.events.ErrorEvent;
import flash.events.Event;
import flash.events.EventDispatcher;
import flash.events.HTTPStatusEvent;
import flash.events.IOErrorEvent;
import flash.events.ProgressEvent;
import flash.events.SecurityErrorEvent;
import flash.net.URLLoader;
import flash.net.URLLoaderDataFormat;
import flash.net.URLRequest;
import flash.net.URLRequestHeader;
import flash.net.URLRequestMethod;
import flash.utils.ByteArray;
import org.bytearray.micrecorder.encoder.WaveEncoder;
import fr.kikko.lab.ShineMP3Encoder;
/**
* ...
* #author Marcelo de Assis
*/
public class Mp3Helper extends EventDispatcher
{
var loader:URLLoader = new URLLoader();
var LOCAL_PATH:String = "http://www.host.com/swf_dir/upload.php"; // URL used to test on Flash IDE
var PRODUCTION_PATH:String = "upload.php"; // URL used to test on server
var mp3Encoder:ShineMP3Encoder;
var urlRequest:URLRequest = new URLRequest();
var urlLoader:URLLoader = new URLLoader();
public function Mp3Helper()
{
urlRequest.url = PRODUCTION_PATH;
urlRequest.contentType = 'multipart/form-data; boundary=' + UploadPostHelper.getBoundary();
urlRequest.method = URLRequestMethod.POST;
urlRequest.requestHeaders.push( new URLRequestHeader( 'Cache-Control', 'no-cache' ) );
var urlLoader:URLLoader = new URLLoader();
urlLoader.dataFormat = URLLoaderDataFormat.BINARY;
}
function upload_s(soundByteArray: ByteArray)
{
var waveEncoder:WaveEncoder = new WaveEncoder();
var wavData:ByteArray = waveEncoder.encode(soundByteArray, 1);
mp3Encoder = new ShineMP3Encoder(wavData);
mp3Encoder.addEventListener(Event.COMPLETE, mp3EncodeComplete);
mp3Encoder.start();
}
function mp3EncodeComplete(event: Event) : void
{
urlRequest.data = UploadPostHelper.getPostData("audio.mp3", mp3Encoder.mp3Data);
urlLoader.load(urlRequest);
urlLoader.addEventListener(Event.COMPLETE, loaderCompleted);
}
private function loaderCompleted(event: Event):void
{
dispatchEvent(event);
var fileLoader: URLLoader = URLLoader(event.target);
trace("loaderCompleted: ", fileLoader.data);
}
}
}
In case of URLLoader security error try to turn on policy logging (PolicyFileLog = 1) in mm.cfg file (check out this article where to locate mm.cfg file in your OS http://jpauclair.net/2010/02/10/mmcfg-treasure/, for win7 the default log location is C:\Users\user\AppData\Roaming\Macromedia\Flash Player\Logs\policyfiles.txt), it will helps in case of Socket as well.
I am relatively new to AS3. I am having a few code issues at the moment i am hoping for some advice on this forum.
Basically, i am creating a form for my website. I have one combobox and I want The user enter their email address and password into the fields.
Then click on the submit button to sign in and to validate what they entered is correct. When i run my code in real-time i get an error
1120: Access of undefined property status_Txt. I dont know what status_Txt means.
I copied a large section of this code from a tutorial i read. My code is below: Can someone please tell me how i can resolve this problem.
import flash.net.URLVariables;
import flash.net.URLRequest;
import flash.net.URLLoader;
import flash.events.MouseEvent;
import fl.data.DataProvider;
import flash.events.Event;
//Building the variables
var phpVars:URLVariables = new URLVariables();
//Requesting the php file
var phpFileRequest:URLRequest = new URLRequest("http://www.example.com");
phpFileRequest.method = URLRequestMethod.POST;
phpFileRequest.data = phpVars;
//Building the loader
var phpLoader:URLLoader = new URLLoader();
phpLoader.dataFormat = URLLoaderDataFormat.VARIABLES;
phpLoader.addEventListener(Event.COMPLETE, showResult);
//Variables ready for processing
phpVars.email = txt_input.text;
phpVars.p_swd = pass.text;
phpLoader.load(phpFileRequest);
btn_one.addEventListener(MouseEvent.CLICK, btnHandler);
function btnHandler(event:MouseEvent):void{
trace("Login success");
}
//Validate form fileds
function showResult(event:Event):void{
status_Txt.text = "" + event.target.data.systemResult;
trace(event.target.data.systemResult);
}
var cbox:Array = new Array();
boy[0] = "Male";
girl[1] = "Female";
c_one.dataProvider = new DataProvider(cbox);
c_one.addEventListener(Event.CHANGE, dataHandler);
function dataHandler(e:Event):void{
trace(e.target.value);
}
}
You're attempting to access an object that doesn't exist. status_Txt is being referenced in:
function showResult(event:Event):void{
status_Txt.text = "" + event.target.data.systemResult; //Either remove this or add a textfield to the stage with the instance name status_Txt
trace(event.target.data.systemResult);
}
I use this code but after publish when I open my file automatically 2 internet browser (www.example.com) are open
package {
import flash.display.SimpleButton;
import flash.display.Sprite;
import flash.net.navigateToURL;
import flash.net.URLRequest;
import flash.net.URLRequestMethod;
import flash.net.URLVariables;
public class bAEForm extends SimpleButton {
public function bAEForm() {
var url:String = "http://www.google.com";
var request:URLRequest = new URLRequest(url);
var variables:URLVariables = new URLVariables();
variables.exampleSessionId = new Date().getTime();
variables.exampleUserLabel = "guest";
request.data = variables;
request.method = URLRequestMethod.POST;
navigateToURL(request);
}
}
}
I want, when I press my button then inter browser (www.example.com) should be open
if you want to navigate to URL when the button is clicked you code should something like this:
package {
import flash.display.SimpleButton;
import flash.display.Sprite;
import flash.net.navigateToURL;
import flash.net.URLRequest;
import flash.net.URLRequestMethod;
import flash.net.URLVariables;
import flash.events.MouseEvent;
public class bAEForm extends SimpleButton {
public function bAEForm() {
this.addEventListener(MouseEvent.CLICK, clickHandler);
}
private function clickHandler(event:MouseEvent)
{
var url:String = "http://www.google.com";
var request:URLRequest = new URLRequest(url);
var variables:URLVariables = new URLVariables();
variables.exampleSessionId = new Date().getTime();
variables.exampleUserLabel = "guest";
request.data = variables;
request.method = URLRequestMethod.POST;
navigateToURL(request);
}
}
}
You can't use navigateToURL function in the constructor
As far as I understand your problem :
You don't want the browsers to be already open but on click of some button.
If that is the case, Currently navigateToURL(request); is in the constructor of bAEForm class.
Move it into a click event, Something like this :
myButton.addEventListener(MouseEvent.MOUSE_CLICK,
function(e) { navigateToURL(request); }, false,0,true);
where myButton is the button instance on click of which you want the browser to open.
I can't seem to get this little AIR app I'm making to work right. There is a single button on the stage, and when the user presses it they are presented with a file browser (FileReference.browse() function). When they select an image file, I want the image to be uploaded to the site imgur.com (http://www.api.imgur.com for those interested).
I'm getting a response from imgur, so I know my app is connecting, but it's returning an error, "No image data was sent to the upload API". Any help is greatly appreciated! (Also I do have an API key, just removed it from this post for obvious reasons :) )
package
{
import flash.display.MovieClip;
import flash.events.*;
import flash.net.*;
import flash.utils.ByteArray;
public class Document extends MovieClip
{
//file browser var
var myFileReference:FileReference = new FileReference();
//file loader var
private var loader:URLLoader;
//string constants
private const API_KEY:String = "******REMOVED*******";
private const UPLOAD_URL:String = "http://api.imgur.com/2/upload.xml";
public function Document()
{
// constructor code
browse_btn.addEventListener(MouseEvent.CLICK, browse);
}
function browse(e:MouseEvent)
{
var imagesFilter:FileFilter = new FileFilter("Images", "*.jpg;*.gif;*.png");
myFileReference.browse([imagesFilter]);
myFileReference.addEventListener(Event.SELECT, selectHandler);
myFileReference.addEventListener(DataEvent.UPLOAD_COMPLETE_DATA, responseHandler);
}
function selectHandler(e:Event):void
{
var vars:String = "?key=" + API_KEY + "&name=name&title=title";
var params:URLVariables = new URLVariables();
params.date = new Date();
var request:URLRequest = new URLRequest(UPLOAD_URL + vars);
//request.contentType = "application/octet-stream";
request.contentType = "multipart/form-data";
request.method = URLRequestMethod.POST;
request.data = params;
myFileReference.upload(request);
try
{
myFileReference.upload(request);
}
catch (error:Error)
{
trace("Unable to upload file.");
}
}
function responseHandler(e:DataEvent):void
{
trace("responseHandler() initaialized");
var res:XML = XML(e.data);
trace(res);
}
}
}
there's actually an example for AS3 on their site:
package
{
import com.adobe.images.PNGEncoder;
import flash.display.BitmapData;
import flash.display.MovieClip;
import flash.events.Event;
import flash.events.SecurityErrorEvent;
import flash.events.IOErrorEvent
import flash.net.URLLoader;
import flash.net.URLLoaderDataFormat
import flash.net.URLRequest;
import flash.net.URLRequestMethod;
import flash.utils.ByteArray;
public class ImgurExample
{
private var loader:URLLoader;
private const API_KEY:String = "<api key>";
private const UPLOAD_URL:String = "http://api.imgur.com/2/upload.xml";
public function ImgurExample() {
loader = new URLLoader();
loader.dataFormat = URLLoaderDataFormat.VARIABLES;
loader.addEventListener(Event.COMPLETE, onCookieSent);
loader.addEventListener(SecurityErrorEvent.SECURITY_ERROR, onSecurityError);
loader.addEventListener(IOErrorEvent.IO_ERROR, onIOError);
// Create a bitmapdata instance of a movieclip on the stage.
var mc:MovieClip;
var b:BitmapData = new BitmapData(mc.width, mc.height, true);
b.draw(mc);
var png:ByteArray = PNGEncoder.encode(b);
var vars:String = "?key=" + API_KEY + "&name=name&title=title";
var request:URLRequest = new URLRequest(UPLOAD_URL + vars);
request.contentType = "application/octet-stream";
request.method = URLRequestMethod.POST;
request.data = png; // set the data object of the request to your image.
loader.load(request);
}
// privates
private function onSend(e:Event):void {
var res:XML = new XML(unescape(loader.data));
trace(res);
}
private function onIOError(e:IOErrorEvent):void {
trace("ioErrorHandler: " + e);
// Handle error
}
private function onSecurityError(e:SecurityErrorEvent):void {
trace("securityErrorHandler: " + e);
// handle error
}
}
}
http://api.imgur.com/examples
I have to work with webservices in Actionscript. I found the following code that allows me to use JSON URLs that implement only the GET method. However, it doesn't work for POST methods (doesn't even enter the "onComplete" method). I searched the net and was unable to find any answers. How can i "POST" JSON data using Actionscript 3.0?
package
{
import flash.display.Sprite;
import flash.net.URLRequest;
import flash.net.URLLoader;
import flash.events.*;
import com.adobe.serialization.json.JSON;
public class DataGrab extends Sprite {
public function DataGrab() {
}
public function init(resource:String):void {
var loader:URLLoader = new URLLoader();
var request:URLRequest = new URLRequest(resource);
loader.addEventListener(Event.COMPLETE, onComplete);
loader.load(request);
}
private function onComplete(e:Event):void {
var loader:URLLoader = URLLoader(e.target);
var jsonData:Object = JSON.decode(loader.data);
for (var i:String in jsonData)
{
trace(i + ": " + jsonData[i]);
}
}
}
}
You need to specify the method used with your URLRequest object. The default is GET. This could be a second argument to your init method:
public function init(resource:String,method:String = "GET"):void {
var loader:URLLoader = new URLLoader();
var request:URLRequest = new URLRequest(resource);
request.method = method;
loader.addEventListener(Event.COMPLETE, onComplete);
loader.load(request);
}
When you call this function you can use the static GET and POST properties of URLRequestMethod rather than just passing strings for a little bit of extra safety.
I'm doing it with
import com.adobe.serialization.json.JSON;
var messages:Array = new Array ();
messages.push ({"nombreArchivo":"value"});
messages.push ({"image":"value"});
var vars: URLVariables = new URLVariables();
vars.data = JSON.encode(messages);
var req: URLRequest = new URLRequest();
req.method = URLRequestMethod.POST;
req.data = vars;
req.url = "crearIMG.php"
var loader:URLLoader = new URLLoader();
loader.addEventListener(Event.COMPLETE, handleServerResponse);
loader.load(req);