Viewers: in countries Watching now:
Take your Android programming skills to the next level with the Android built-in framework that enables local data management in text files and SQLite-based relational databases. This course shows you how to create datacentric apps for Android devices, using SQLite, Java, and the built-in android.database packages. Author David Gassner describes how to define shared preferences, work with JSON and XML files in internal and external data stores, and create new local SQLite databases.
I'll go to my MainActivity class and scroll down toward the bottom of the code and find the method createFile. In this version of my createFile method I already have code to create a file in internal storage--that is, a file that's local to the current application. But I'll add code here to create a JSON data packet. I'll place the cursor above any of the other code in the method, and I'll start by creating an instance of the class JSONArray. It's spelled J-S-O-N, in all uppercase, and then Array.
And I'll name the object dat'. A JSONArray is a resizable array. You can add as many objects as you want to it and then it will automatically serialize the data into a JSON notation string. I will instantiate the object with the class's constructor method, using new JSONArray. Next, I'll declare an instance of a class called JSONObject, and I'll name that tour. I'm not instantiating it quite yet. I'm just going to start with declaring it.
A JSONObject is a dynamic object that implements the Java Map Interface. You can add values to the JSONObject as key-value pairs. The keys are always strings, but then the values can be anything you want. I'll create a single tour instance. I will instantiate the object using tour = new JSONObject. Then I'll add a couple of properties. I'll name my two properties tour and price.
This is telling me I need to add a throws declaration. Whenever you start creating JSON objects or JSON arrays, you might throw an exception called JSONException. I'll add that to the throws clause by clicking on the icon and doing a quick fix. That adds JSONException to the list of exceptions that might be thrown from this method. I'll come back to my call to the put method and I'll duplicate that. Then I'll change the key for the second value from tour to price.
For the second tour, the tour property will be Death Valley and the price will be 600. And for the third one, the tour name will be San Francisco and the price will be 1200. Notice that I'm not including a comma here. This is a pure numeric literal. And because we're working in Java 6 compatibility, you can't add an underscore as you might in Java 7. That won't be recognized correctly. Just leave it as is, without the underscore or any other notation, and it will be correctly data typed as a number.
So now my JSONArray is ready to use. And I'll come down here where I'm setting a string variable called text, and I'll get its value by calling the data object's toString method. And that serializes the value to a string, and the string can then be saved to disk. I'm going to change the name of my file. And instead of myfile.txt, I'll name this file tours. I won't use any file extension. Finally, instead of just telling the user that the file was written to disk, I'll actually display the content that was created.
After File written to disk, I'll add a line feed with a \n, and then I'll append to that data.toString. Again, I'm serializing a JSON string. I'll save my changes and run the application in the emulator. When the application opens, I'll click Create file, and I see the message that the file was written to disk, and I see the JSON notation. The square brackets wrapping the entire string mean that it's an array, and each JSONObject is notated with curly braces.
The string values have quotes and the numerics don't. It's pretty easy to read, and it's been written to disk and is ready to use. I'll add code to retrieve and read the file from disk. I'll go back to Eclipse and I'll move down to the method readFile. First, I'll change the name of the file that I'm reading. Once again, instead of myfile.txt, I'll use tours. Then I'll move the cursor down to after the content has been read. I'm going to move a couple of lines of code around.
Before I start processing the JSON content, I want to make sure that I've closed my strings, so I'll move the calls to the close methods up a bit and then I'll place the cursor after the close methods and before the call to displayText. In order to deserialize from a string to a JSON array, declare the JSONArray object, and once again, I'll call it data, and I'll once again use the JSONArray constructor method, but this time I'll pass in the string that contains the JSON-notated content.
I'll pass in b, the StringBuffer, .toString. And now, I've deserialized the content and I'm ready to deal with it as a Java object. Just as I did with the createFile method, I'll do a quick fix and add JSONException to my throws clause. I'll move down to after I've deserialized the data. I'll loop through the array. I'll use a for loop, iterating over an array. My temporary variable will be i, and I'll change the array that I'm reading to data.
Within the for loop, I'll create a string, and I'll name it tour, and I'll call the JSONArray class's getJSONObject method. And I'll pass in the current index that I'm looping on. So now I'm returning a JSONObject, and I want to retrieve just the string value representing the tour name. So, from the JSONObject, I'll call the getString method. Notice that there are methods for getString, getBoolean, double, int, and long. These are the data types that are supported by JSON notation.
I'll call getString, and I'll pass in the key tour. Remember, that's the key that I used when I created the JSON notation. I still have one error in the for loop, and that's because the code template I used when I created my for loop treated length as a property of the data object. But in the JSONArray, it's a method. So, I'll move my cursor after the length property and press Ctrl+Space, and that correctly fills in the method call. So now, I'm retrieving a string for each tour in the array.
I'd like to concatenate them all together into a single string. I'll place the cursor above the for loop, and I'll create a new StringBuffer object, which I'll call toursBuffer, and I'll instantiate it with the constructor method. Then I'll move the cursor back to within the for loop, and I'll take that tour string and I'll append it. I'll call toursBuffer.append and I'll pass in tour, plus a line feed. Finally, I'll wrap up the coding by going down to the call to displayText, and instead of outputting the rawBuffer, now I'll output the toursBuffer.
So, let's again review the code. I'm starting by using streams to read the content into memory. Then I'm deserializing from the string into a JSONArray object. Then I'm looping through the JSONArray and getting the data from the objects one at a time. And finally, I'm outputting the content that I've retrieved to the screen. I'll save and run the application in the emulator again. Remember, I've already created the file in persistent storage. I don't need to create it again.
I'll click Read file, and there's the result. I've retrieved the data, and I'm displaying it on the screen. So, that's a look at how you can both create files and read files using the JSON format. The JSON format is one of the smallest, most concise formats that you can use. It's incredibly fast on an Android device, and all the tools you need to use are included in the Android SDK.
There are currently no FAQs about Android SDK: Local Data Storage.
Access exercise files from a button right under the course name.
Search within course videos and transcripts, and jump right to the results.
Remove icons showing you already watched videos if you want to start over.
Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.
Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.