0

I have an app that retrieves XML From a server I run... Im trying to limit the calls to the server (if possible) to improve responsiveness of the app and decrease the likelihood that the user will get errors if the data stream doesnt make the trip successfully for whatever reason -- i get some intermittent network monitor errors, it could be due to some wifi channel conflicts I think I have, but its still is a potential weak point in the overall process and if I can work around that need to call to the server each time the user performs an 'action' within the app.

what I'd like to do is use the local storage on the device to (upon launch of the app) retrieve and save the XML data, then change my httpservice url the local file.

I have some code I used before to successfully write and read config settings to a local text file, so Im pretty sure this can be achieved, just not sure how exactly to go about it.

thanks for any light someone can shed on this or any code examples anyone can share.

tamak
  • 1,541
  • 2
  • 19
  • 39

1 Answers1

0

Load it by default from the applicationStorageDirectory if it doesnt exist donwload and save the xml to the applicationStorageDirectory

to load:

var xmlFile:File = File.applicationStorageDirectory.resolvePath("data.xml");
if (xmlFile.exists)
{
   var fileStream:FileStream = new FileStream();
   fileStream.open(xmlFile, FileMode.READ);
   var xml:XML = XML(fileStream.readUTFBytes(fileStream.bytesAvailable));// you will want to declare outside this scope, its just for show
   fileStream.close();
}else{
  downloadAndSaveXML();// if it doesnt exist dowload it then save it and call load again!
}

to save:

//download .xml first and save in the onComplete handler, im calling the file "xml"
var file:File = File.applicationStorageDirectory.nativePath("data.xml");
var stream:FileStream = new FileStream();
stream.open(file, FileMode.WRITE);
stream.writeUTFBytes(xml.toXMLString());
stream.close();

get the idea?

You will also need this compiler argument so the HTTPService doesnt look online for the xml

-locale en_US -use-network=false

You could not bother with dowloading the file in the firstplace and just embed it in the Application

here a link to the topic

M4tchB0X3r
  • 1,531
  • 1
  • 15
  • 28
  • other than having to piece it togehter to suit my needs the code you provided makes absolute sense and is simpler than I thought it would be... brilliant... awesome - thanks... will try this out. the data will need to be obtained from the server periodically due to the rate of updates to the Live database on the server -- I'll start out with just making sure the locally saved copy is no more than 24 hours old. – tamak Mar 14 '13 at 01:37
  • U should not use the compiler argument then and just use the local xml file. – M4tchB0X3r Mar 14 '13 at 01:59
  • the issue Im struggling with now is how to take the XML data that is obtained through the httpservice and place that in a local file or save it as a local file. – tamak Mar 14 '13 at 03:03
  • In the ResultEvent of the HTTPService. `var xml:XML = response.result as XML;` You could also use an URLRequest instead of the HTTPService – M4tchB0X3r Mar 14 '13 at 10:31