0

I'm building an android app which finds the nearby wifi hotspots. I have an xml file which includes GeoLocations of the hotspots, the xml file is located in the res/xml folder of the application. I would like to download an updated xml file when application starts. I have two questions:

  1. Shoud I download the xml file into Internal Storage or External Storage and why? If I should download the xml file into Internal Storage, how can I do that?
  2. I would like the user to be able to use the initially embedded xml file when the application first starts and switch to the newly download xml file when the download finishes. How can I achieve that seamlessly?

Thanks

StarCub
  • 4,141
  • 7
  • 41
  • 58
  • Did you consider using SQLite? It's much faster than xml files. You can start by checking if your database is empty, put your xml data there if it is, and then download the rest of data from server and update it every time you need. – Michał Klimczak Apr 20 '12 at 10:11

1 Answers1

1

Take a look at Google's page regarding data storage on Android. http://developer.android.com/guide/topics/data/data-storage.html

You can download the XML file in a pretty straightforward matter. Another question like yours was asked here:

Download a file with Android, and showing the progress in a ProgressDialog

In that example, the file gets saved to the SD card. Be sure to change the OutputStream pointing to the SD card, so that it points to the private storage, as the reference page describes:

FileOutputStream fos = openFileOutput(your_xml_filename, Context.MODE_PRIVATE);

To make the switch to the new XML as simple as possible, I'd encapsulate all XML-related functionality in one class and provide a simple method for switching to the new XML. For example,

class XMLUser {
    private Location someLocation;

    public XMLUser(String pathToXml) { 
        readxmlAndSetLocations(pathToXml);      
    }
}

Then you'd initialize a new XMLUser object whenever you'd need to change the XML file. Like so:

XMLUser xmlStuff = new XMLUser(PATH_TO_RES_FILE);
//... do something with it
//... update found
XMLUser xmlStuff = new XMLUser(PATH_TO_PRIVATEDATA_FILE);

Another way would be to use the same object and just update the locations from the XML file:

  class XMLUser {
        private Location someLocation;

        public void updateLocations(String pathToXml) { 
            //Read your xml here and update the locations accordingly.      
        }
    }

e.g.

XMLUser xmlStuff = new XMLUser(PATH_TO_RES_FILE);
//... do something with it
//... update found
xmlStuff.updateLocations(PATH_TO_UPDATED_XML);

Your questions allow for a wide variety of answers so I can't give a quick and exact one.

And yes, I do recommend saving the XML data to the internal storage. The XML file should be small enough in size to not pose problems, and logically, it's related to the internal functionality of the app. With which the user should not tamper. Plus, the internal storage is not removable, so in case the user changes the SD card, the updated data will still be there.

Community
  • 1
  • 1