The best way to cache an XML / JSON response in my application. Full text file?

I am currently using XML or JSON web services in various applications.

I just would like to know what would be the easiest way to cache theses of the answers in text files.

I was thinking about databases, but it seems pretty complicated!

So, I would like to do it

  • to save these xml / json files in phone memory, should i do something like this?

    String FILENAME = "MyXmlFile";
    String string = "hello world!";
    
    FileOutputStream fos = openFileOutput(FILENAME, Context.MODE_PRIVATE);
    fos.write(string.getBytes());
    fos.close();  
    
  • In the onCreate of my application, I would like to parse this file. Since all the tutorials show how to parse a file from a web URL, I never found a way to parse an internal text file. Anyone have any advice on this?

Thank.

+5
3

XML/JSON. openFileInput FileInputStream. .

0

SharedPreferences, XML . JSON/XML, /, pref. onCreate() / pref. :
http://android-er.blogspot.com/2011/01/example-of-using-sharedpreferencesedito.html

+1

Another way is to use awesome http-request . It allows you to use ETag to check if a response is responding (HTTP 304 Not Modified). If you have not changed, use JSON from the file cache, otherwise make a request for a new resource.

File latest = new File("/data/cache.json");
HttpRequest request = HttpRequest.get("http://google.com");
//Copy response to file
request.body(latest);
//Store eTag of response
String eTag = request.eTag();
//Later on check if changes exist
boolean unchanged = HttpRequest.get("http://google.com")
                               .ifNoneMatch(eTag)
                               .notModified();
+1
source

All Articles