After I upload a photo on a desktop facebook application i need to store it's post id in a database. From the facebook ActionScript SDK documentation:
api() method
public static function api(method:String, callback:Function, params:* = null, requestMethod:String = GET):void
Makes a new request on the Facebook Graph API.
Parameters [...]
callback:Function — Method that will be called when this request is complete The handler must have the signature of callback(result:Object, fail:Object); On success, result will be the object data returned from Facebook. On fail, result will be null and fail will contain information about the error.
So I implemented my callback function as follows:
protected function handleUploadComplete(response:Object, fail:Object):void{
status = (response) ? 'Success' : 'Error';
var file:File = File.desktopDirectory.resolvePath("Log.txt");
var stream:FileStream = new FileStream();
stream.open(file, FileMode.WRITE);
stream.writeUTFBytes(response.toString());
stream.close();
}
The problem is that response or response.toString() both return "[object Object]", and I was hoping for something more like "id: somenumber".
What am I doing wrong?
If everything has worked the response will contain a property called id that is your post ID. Otherwise it will be null and the fail object will be populated.
protected function handleUploadComplete(response:Object, fail:Object):void
{
var status:Boolean = response != null;
if(status) var id:String = response.id;
//do whatever you want
}
Related
Struggling to find any documentation to do a simple GET request to a URL to return JSON in Actionscript / Flex.
Anyone know
Use the URLLoader API.
var myJSON :Object; //listing as per "key:" with "value"
var str_JSON: String = ""; //if you need text String not data Object
var siteloader :URLLoader = new URLLoader( new URLRequest( your_URL ) );
siteloader.addEventListener(Event.COMPLETE, whenSiteLoaded);
function whenSiteLoaded( evt :Event ) :void
{
myJSON = JSON.parse(evt.target.data); //get into Object
str_JSON = evt.target.data; //get into String
//... any other code
}
You should add to the above the listeners for error conditions (eg: a "file not found" situation)
I've asked my client to share his database login and password but he can't give me full access to his database (security reason I suppose).
He told me to use a REST/JSON service that allows to post the data via this url with a specific key that allows him to identify all the datas coming from my app.
Here's what I did :
var urlRequest:URLRequest = new URLRequest("the_url_using JSON service");
urlRequest.method = URLRequestMethod.POST;
var urlvars: URLVariables = new URLVariables;
urlvars.observer_name = "Test Coco";
urlvars.observation_number = "5433";
trace("urlvars = "+urlvars);
urlRequest.data = urlvars;
var urlLoader:URLLoader = new URLLoader();
urlLoader.addEventListener(Event.COMPLETE, onComplete);
urlLoader.load(urlRequest);
It's working, as it's sending the data, but the data format seems to be incorrect..
the url returns this error : "Observer name is Missing"
And the "trace (urlvars)" output :
urlvars = observer%5Fname=Test%20Coco&observation%5Fnumber=5433
So I think the problem come from the special character or something like that (as you can "observer_name" results by "observer%5Fname" and we can see a lot of %5")
Any idea how can I solve this ?
JSON string is a string representation of a generic object. Basically you go:
var anObject:Object =
{
"observer_name": "Test Coco",
"observation_number": 5433
};
or you can construct it
var anObject:Object = new Object;
anObject['observer_name'] = "Test Coco";
anObject['observation_number'] = 5433;
and then you convert it to String and attach to request
var jsonString:String = JSON.stringify(anObject);
urlRequest.method = URLRequestMethod.POST;
urlRequest.data = jsonString;
Read more about it: https://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/JSON.html
Keep in mind that I don't know the specifics of that REST server of yours and the code above just might not work as it is. I only explain how to send a JSON string as a POST request.
I am having trouble posting an xml file with the URLLoader.
After it loads it seems that there is no XML attached to the request, could any
please clarify what i am doing wrong here (banging head against the wall)
// code
private function postXmlFiles():void
{
var a:XML = new XML(new EmbeddedXML());
var r:URLRequest = new URLRequest("http://vms.api.apic.co/rest/v3/addmedia");
r.data = a;
r.method = URLRequestMethod.POST;
r.contentType = "text/xml";
l = new URLLoader();
l.dataFormat = URLLoaderDataFormat.TEXT;
// Handlers
l.addEventListener(Event.COMPLETE, on_complete);
l.load(r);
}
private function on_complete(e : Event):void
{
trace('loaded!:' + l.data); // this is returning "loaded!: No xml file attached to the request."
}
I tested your code, replacing one string:
var a:XML = new XML("<test/>");
Trace output changed, indicating l.data field actually contains something.
I also used debugging proxy, and examined request body, it does contained my <test/> string.
You should test your new EmbeddedXML() call to see what it returns.
I have a variable
var qstAccessCode:String = "default";
and a loader with URLRequest
var qst:XML;
var qstLoader:URLLoader = new URLLoader();
qstLoader.load(new URLRequest("http://dl.dropbox.com/u/44181313/Qaaps/Audio/" + qstAccessCode + ".qst"));
qstLoader.addEventListener(Event.COMPLETE, processQST);
function processQST(e:Event):void {
qst = new XML(e.target.data);
trace("QST loading");
}
I would like to use the value of qstAccessCode to complete the URL (so I can change the URL based on user input - if no input then use "default") but I get an error:
"1120: Access of undefined property qstAccessCode"
Is this to do with scoping? How can I complete the URL? Thanks in advance.
Edit: I haven't been able to get clear on this, so I'm also going to look at generating the complete URL from the user-input function and see if I get the URLRequest to pick it up as a variable. If there are any further comments on the original idea I will be very grateful to read them. Cheers.
Edit: #Moorthy I have qstAccessCode defined like this:
var qatAccessCode:String = "default";
var stageText:StageText = new StageText();
stageText.returnKeyLabel = ReturnKeyLabel.GO;
stageText.stage = this.stage;
stageText.viewPort = new Rectangle(225, 765, 200, 35 );
stageText.addEventListener(Event.CHANGE, onChange);
function onChange(e:Event):void
{
qatAccessCode = stageText.text;
trace(qatAccessCode);
}
It traces keyboard entry when I test movie (Air 3.2 for Android).
qstAccessCode should be defined in the same scope as the URLRequest.
You must defined property qstAccessCode like:
var qstAccessCode:string;
qstAccessCode's value is your url address.
I am trying to upload a photo from an Adobe AIR application to yfrog's api, http://code.google.com/p/imageshackapi/wiki/YFROGuploadAndPost. I have the following code:
public function upload(file:File, msg:String, username:String, password:String):void {
var vars:URLVariables = new URLVariables();
vars["username"] = username;
vars["password"] = password;
vars["public"] = "yes";
vars["key"] = API_KEY; //API_KEY is a constant string that holds my developer key
vars["message"] = msg;
var request:URLRequest = new URLRequest("http://yfrog.com/api/uploadAndPost");
request.method = URLRequestMethod.POST;
request.contentType = "multipart/form-data";
request.data = vars;
file.upload(request, "media");
}
When I run this code, yfrog returns 404 status. This seems to only happen if I do a media file upload with the api. If I use a "url" upload to the same api url - everything works. Has anyone else gotten a "media" file upload to work? If so, how would you change the code?
Looks like that API has been replaced as of today with the OAuth Echo method
http://code.google.com/p/imageshackapi/wiki/TwitterAuthentication