Caching Objects parsed from XML in Android - android

I have an Android application which talks to a public Data-API by calling URLs. The API returns XML which describes search results or detailed Data of a particular dataset.
In my Application i retrieve this data and parse it to Java Objects to display them in a ListView for example.
edit: Just to make it clearer: In my Application you can search for music Artists and get their whole discographic information. I Retrieve the list of Releases and display them in a ListView. (No problem right here, because the results of the same search request can change any minute i have to retrieve this data everytime a search request is issued).
Now i have a list with all the LPs the Beatles produced (for example). I can then click one particular LP and view the Details such as the Release Year and the Tracklist.
What i want to cache is the Details data and i'm currently thinking of which is the best way to do this. I thought of:
Retrieving the XML data once and store the XML Data in the SQLite Database (that would imply, that i have to parse the data everytime i want to access it again).
Retrieving the XML data once, parsing it once and somehow store the serialized JavaObject into the SQLite Database as ByteStream. (so all the time consuming work would be done just once).
What do you think is the best version or is there maybe another better way to achieve caching the results?

serializing an object would be quick solution but that could not be effective solution. Every a time you need to load entire object, while in this case if you are storing your data set into database then, using cursor/queries data handling will be smoother.
CursorAdapter will allow you to plug database cursor directly to list in GUI. I would suggest you to use database approach.

Related

Best pratice to store data fetched from server into android app

For my app I have a simple API to push/pull JSON data from server.
Suppose I fetched a list as a JSON array
I saved that JSON array into file "my_list.json" with MODE_PRIVATE access. I am saving the list to show when app is just started without network connection.
Also I am showing the list data (JSON array converted into Object array) in a List View.
In this listview User can select an item and delete that particular item (deleting through API calls, so in server DB that item is deleted) and that is disappeared from list.
Since I didn't delete that item in saved file "my_list.json" deleted data is appearing again when app is started.
I know maintaining a sqlite DB in my app solves this problem. But without using DB -
Is it good practice to save list data as JSON into file in android app ?
What is the elegant solution for such cases without using database in my app?
To be able to alter the data the way you want the best way is to use SQLite database, which will allow you to query, update and delete the records you save .. which is a way better solution than using shared preferences or files.
Here's an excellect tutorial of how to implement it

Data to Android app from web server

My app pulls data from the web server. The results are given to a ListView.
Question 1: In what format should I transmit data to minimize data usage ?
Question 2: Should I use a SqlLite database to store the results from the server and feed it to the ListView or can I load it a few (say 100) values to the an ArrayList and set it as the data source for the ListView for a better performance ?
Question 1: In what format should I transmit data to minimize data usage ?
Answer 1: Its depends on your server side data format. If it is in Xml format then you have to
use xml parsing. If its in Json format then you have to use JSON parsing. I suggest to you use JSOn parsing.
Question 2: Should I use a SqlLite database to store the results from the server and feed it to the ListView or can I load it a few (say 100) values to the an ArrayList and set it as the data source for the ListView for a better performance ?
Answer 2 : You can use HashMap arraylist to store your data and retrieve as easily from it. But if you have a bunch of data earlier said by you then you need to use SQLITE database. In which you have to store your all data in it. and after that retrieve from the Database.
Q1: you can use JSON or Xml for data transferring because these both are standards but JSON is widely used for data driven applications.
Q2: For Large data you can directly load data to list using lazy loading technique, or this is totally dependent on a person, if you store in database to use this data on multiple locations and for data storage, this depends on you
Data format
The JavaScript Object Notation (JSON) is widely used in transferring data over the net
Storage
That depends on what you're trying to do - if the data is only temporary in nature, what you're suggesting works. If it needs to be stored across application starts, you will need some kind of persistence. A database would be one way to do that.
Remember though, that if you place your data loading code in your activity's onCreate or onResume method (or anywhere in startup callbacks of the activity lifecycle), it will get loaded everytime your activity is created - even if the user just flipped the device. If the data takes long to load (which over the network can always be the case, even if it's just a few bytes), this can result in very bad user experience.
One way to deal with this would be using a custom Loader, which can exist separate from your activity and would be able to cache your previously loaded data.
Conceptually, doing this would require you to extend Loader, and override the onStartLoading method to begin loading your data from the network. You should probably also override onStopLoading and onCancelLoad to keep your app from needlessly shoveling data over the connection even if it's not wanted anymore.
Having done this, you would provide LoaderCallbacks (as shown in the link I gave you), and instead of creating a new CursorLoader in the onCreateLoader callback, create an instance of your own custom loader class.

Storing a large amount of backend data in Android

I am developing a places of interest app which will display the list of places of interest in a location.
When user chooses one, it will display more information and address etc.
How do I store all this data? Currently I am using a text file to store all the data and subsequently when user chooses a place, it will parse the text file and retrieve the necessary data for display.
Any advice on what is a better way to do this? I looked at SharedPrefs, but it is more like storing "key-value" pair and in this case I need to store a large amount of data.
I want the info to be available even when the device is offline, thus I can't download from an online server upon request.
Any other way to do this?
You may store it to XML file using XML serializer, here is very good tutorial for learning that,
http://www.ibm.com/developerworks/library/x-android/
and it can be easily parsed using Java XPath Api. Have a look at this at parsing XML files
http://www.ibm.com/developerworks/library/x-javaxpathapi/
Use SQLite
It can store large data.
It is available offline.
All your problems will be sorted out.
Hre we have a wonderful tutorial for sq-lite
http://www.vogella.com/articles/AndroidSQLite/article.html
How about a relational database?
http://developer.android.com/training/basics/data-storage/databases.html
Take a look at Serialization. If you do not need database access, you could define a class what holds every information you need. Then, you can do the following:
when you need to save the datas, you serialize your object, dumping its content to a file, for example on the SD card
when you want to load the datas, you just load the above mentioned file, and get back everything from the dumped file
I am using this method in my app to cache some datas that would need internet access, so the user can still view it, and with proper implementation, this can work very nicely.
Use database, create table and insert all the data in it. When you need the data just fire the query, and you are done.
SQLite is fine for Android.
Depending on the type of data you want to store, you could use a SQLite Database (provided with Android) if it has a normal database structure. You could Serialize your data and save it in a raw or encrypted file, making you data implement Serializable.

Working with lists of data from server

I am going to populate data from server to a listview (endless). What is the best practice for doing this? Should I cache this data to local db?
Update: Usually list contains not much data about each item. But those item types (say it a "company info") I am also going to use for favorites which should always be stored locally. So I would like to reuse the same table for favorites and items temporally downloaded from server.
Firstly as per per your question please be specific about your requirement .
To handle data from server to listview .. The data that is obtained from server ( i.e parsed json , xml etc) need to sotred in some collection say ArrayList or Hash map etc.
These list can be populated from the above collections.
But if you intend to have permanent storage for the data in the app i.e offline browsing etc than you should save the data in sqlite db and use Cursor Adpater etc for showing the list.
So, the way you use the android recommended widgets and views depends upon the your requirement in mind + the optimized way to handle those things in efficient way.

Parsed Json (jackson) to objects data saving

To get Data for my application, I parse a Json file, with Jackson, to (lists of) custom Objects. When I start my app, I check if there is a new Json file available and ask the user if they want to download it, else I use the "old" Json file. But every time I start my app I parse the Json. Then I use the Application Class to save my list of objects an go to my data when I want, most of the time I only need one object.
From the huge list, with multiple layer nested object, I create a simple "flat" arraylist of custom objects in which I put only the data I need to create listviews (name, id, second text and url of picture). When something is clicked, i use the id to get all the data.
Parsing this whole Json file every time is pretty time consuming and makes the startup time of my application long. Ofcourse, this sucks.
And having this huge list of custom objects saved in Application Class fills a lot of memory of my device, and sometimes after some use the class gets killed, and I need to reparse again.
Is there a way I don't need to reparse all my data?
I hoped for a process like this:
new Json file
first time parse total JSON to list of multilayered custom objects
create simple list for listviews
delete/clear the big list
some clever way to get only one of the giant items, without keeping the whole list in my memory. (maybe something with Jackson).
on destroying of the application maybe save the simple list, i read something about parceable or serializable?
Anyone knows how to achieve this?
Or has an other awesome idea?
Jackson has a streaming api. Also you can parse the json in a AsyncTask (in the background) and update your user interface once the new data is ready
I'd probably store the data in a SQLite database, in line with how the Android platform was designed.
As an alternative to streaming Jackson API (which is very fast, but still has to scan through most of the content), perhaps you could just save things in different files, one per entry? Or, if there is a way to group things, in multiple files each having some subset?
Of course, if you really have tons of entries, use of SQLite as Bruce suggested makes lots of sense.

Categories

Resources