PDA

View Full Version : External XML in AIR


gyzhor
08-05-2008, 11:43 PM
I'm loading XML and CSS from a web location into a desktop AIR (publishing it directly out of Flash, not using Flex).
Everything tests okay in a regular Flash 9 player, but when I publish to AIR it all breaks.

My code is below.
Anyone see anything causing the problem? Is it a sandbox or security issue?
The same air is ignoring a LocalConnection from another open desktop movie, if that's any clue.

var cssLoader:URLLoader = new URLLoader();
var cssRequest:URLRequest = new URLRequest("http://www.mywebsite/myStyle.css");
cssLoader.addEventListener(Event.COMPLETE, cssLoaderComplete);
cssLoader.load(cssRequest);
function cssLoaderComplete(evt:Event):void{
var css:StyleSheet = new StyleSheet();
css.parseCSS(URLLoader(evt.target).data);
myTextField.styleSheet = css;
}
var xml:XML;
var urlRequest:URLRequest = new URLRequest("http://www.mywebsite.com/myXMLText.xml");
var urlLoader:URLLoader = new URLLoader();
urlLoader.dataFormat = URLLoaderDataFormat.TEXT;
urlLoader.addEventListener(Event.COMPLETE, urlLoader_complete);
urlLoader.load(urlRequest);
XML.prettyPrinting = false;
function urlLoader_complete(evt:Event):void {
xml = new XML(evt.target.data);
myTextField.text = xml.toXMLString();
stop();

evride
08-06-2008, 12:35 AM
ya you can try putting a crossdomain.xml file on your server.


<cross-domain-policy>
<allow-access-from domain="*" />
<site-control permitted-cross-domain-policies="all"/>
</cross-domain-policy>


but if that doesn't work, is there an error message that it posts when you try to publish to AIR?

gyzhor
08-06-2008, 05:43 PM
Thanks evdog, I'll definitely try that.
And no, I'm not getting any errors - it's just not loading the XML.

Later last night, though, I took this same script and made a much simpler movie out of it, and the XML loaded into it okay.

It was a bit wonky - sometimes it loaded without the CSS, sometime it loaded perfectly, sometimes it wouldn't load at all.

So now I'm thinking it isn't the script at all, but maybe the frames I'm loading them into? Since (at least from my experience) AIR eats up more RAM than a regular swf, maybe it's just taking longer to reach the online files, and I'm simply not accommodating that before stopping the frames?

Maybe I should have a few frames loop until the XML is fully loaded?
I dunno - looks like I'll be experimenting a while longer.

~gyz

gyzhor
08-06-2008, 07:04 PM
Okay, now I feel dumb - I'd placed the loadXML functions inside of the LocalConnection functions, so if the LocalConnection fails, the XML doesn't even try to load.

It's always the little things that trip me.

But that doesn't explain why I can't get the LocalConnection to work.
It works as a regular AS3 flash, but exported to AIR, it fails.

I've had security and sandbox issues with this before, so does anyone have input on that? Currently the sender is located on my desktop. While testing, I believe the swf would be located in the same folder. Once exported, the AIR would be running from its new location in the programs folder, but I didn't think I'd need to create any security protocols for a connection on the same machine.

The sender will eventually live online, so I guess it'd be best to start testing that now. Maybe testing from the online location with a simple Security.allowDomain("*"); will fix it.

Here's the code for anyone interested in taking a peek:

//LISTENER MOVIE
import flash.events.Event;
import flash.net.LocalConnection;
var myFlashVar:String;
var incoming_lc:LocalConnection = new LocalConnection();
incoming_lc.allowDomain("*");
incoming_lc.connect("myLC");
function myFunction(incomingFlashVar:String):void
{
myFlashVar = incomingFlashVar;
}



//SENDER MOVIE
import flash.events.Event;
import flash.net.LocalConnection;
var outgoing_lc:LocalConnection = new LocalConnection();
function mySender(event:Event):void
{
outgoing_lc.send("myLC", "myFunction", originalFlashVar);
}
stage.addEventListener(Event.ENTER_FRAME, mySender);

evride
08-06-2008, 09:35 PM
sorry, i've never used localconnection before, about the only thing i can do is direct you to a flex/AIR localconnection example.

http://blog.everythingflex.com/2008/01/11/more-fun-with-air-localconnection-source-included/

gyzhor
08-06-2008, 10:47 PM
I finally got it to work, though I'm scratching my head as to how.
I guess I just fiddled long enough for something right to have happened.

So, yeah, not sure I know what happened, but it could have been the inclusion of incomingLC.allowDomain("*"); in the receiver movie and by adding an underscore to the connection name in both movies, as in:
incomingLC.connect("_myLC");

I could've swore I'd already tried that, but maybe I had something spelled wrong, or slightly off somewhere.

~gyz

evride
08-06-2008, 11:40 PM
lol. well im glad you got it to work. definitely the best way to learn is to just fiddle with something till it fits together.

gyzhor
08-06-2008, 11:50 PM
Amen to that! :D

Thanks again!
~gyz