Forums | Users |
Search | Signup | Login

ActionScript / Flex / AIR sample

Subscribe to ActionScript / Flex / AIR sample 1 post, 1 voice

 
Avatar James 2 post(s)

Hey guys, Seeing as there’s no Flex, Flash or AIR examples around that I can see, I thought I’d post some sample code that I’ve whipped up. This connects to, and traces out the name of your project.
It’s a Flex MXML file that will work fine for Flex and AIR applications (although the AIR one will display nothing visually unless you convert it to a WindowedApplication).

Just replace username, password and subdomain (obviously). In an AIR application there is no security sandbox, so it works fine – in a Flex application, with Flash Player 9 and above there is a sandbox that prevents the Flash Player from passing header variables without a crossdomain.xml file, so you will be prompted for a username/password login before the results are retrieved. (http://kb.adobe.com/selfservice/viewContent.do?externalId=kb403184&sliceId=2)

<?xml version=“1.0” encoding=“utf-8”?>
<mx:application initialize="init();" creationcomplete="srv.send();" xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute">
<mx:script>
<![CDATA[
import mx.rpc.events.ResultEvent;
import mx.rpc.events.FaultEvent;
import mx.utils.Base64Encoder;

private var baseUrl:String = “http://phprestsql.sourceforge.net/tutorial/user”;
private var auth:String = “##username##:##password##”;

private function init():void{
var encoder : Base64Encoder = new Base64Encoder();
encoder.encode(auth);
srv.headers[“Authorization”] = "Basic " + encoder.toString();
srv.headers[“Accept”] = “application/xml”;
srv.headers[“Content-type”] = “application/xml”;
}

private function onSuccess(e:ResultEvent):void {
trace(e.statusCode)
trace(“Success”);
trace(e.result);
trace(XML..title)
}

private function onFault(e:FaultEvent):void {
trace(e.message);
trace(“Fault”);
}

]]>

</mx:script> <mx:httpservice result="onSuccess(event);" resultformat="e4x" url="http://##yourdomain##.unfuddle.com/api/v1/projects.xml?formatted=true" method="GET" id="srv" fault="onFault(event);"> </mx:httpservice> </mx:application>