Week 6

Lecture date: May 1

You'll learn more about these topics:

  • Shared Preferences

  • File Input/Output

  • JSON

  • XML

To download the slides I have used to teach about these topics, please click here.

Shared Preferences

Imagine you are building an app that fetches data from an external API. At the moment, the app automatically fetches data each time the user opens up the application. Wouldn't it be better to save the data to the user's physical device and use that data rather than downloading all the time. Only when the user requests that new data be downloaded will the fetching of the data takes place again.

To save data to user's storage, you can use what's called Shared Preferences.

Simply speaking, Shared Preferences utilizes XML as its form of record. As an XML file, the data to be stored is in the format of a key-value pair. The benefit of using Shared Preferences is that you can save pretty much any kind of data that you want as long as the data is simple (like keeping track of a game's high score).

File Input / Output

Utilizing what we will learn about InputStream, OutputStream, BufferedReader, and BufferedWriter, we can gain an understanding of how to access Android's internal and external storage as well as caching system.

Refer to the lecture slides (link provided above) to learn more about each topic in detail.

JSON Object Notation

Everybody who signs up for this course surely knows how JSON work and potentially have worked with them before. So there's no need to go over the benefits and cons of using JSON.

But I should mention, however, that JSON is used by popular databases like CouchDB, MongoDB, RavenDB, and lots more because of its simple nature and easy-to-read form.

Here's a simple example of a JSON object:

{
    "glossary": {
        "title": "example glossary",
        "GlossDiv": {
            "title": "S",
            "GlossList": {
                "GlossEntry": {
                    "ID": "SGML",
		    "SortAs": "SGML",
	            "GlossTerm": "Standard Generalized Markup Language",
		    "Acronym": "SGML",
		    "Abbrev": "ISO 8879:1986",
		    "GlossDef": {
                        "para": "A meta-markup language, used to create markup languages such as DocBook.",
			"GlossSeeAlso": [
                            "GML", 
                            "XML"]
                },
		"GlossSee": "markup"
                }
            }
        }
    }
}

To read and write JSON object in Android, you can use a built-in library called JsonReader and JSONWriter.

More information on JSON:

Last updated