Android: compare json data with database - android

in my Android app, I have a List of objects (Employees) obtained from a JSON on a server. Whenever I open the app, it parses the JSON, creates the ArrayList and populates RecyclerView. I would like to create a database and load the data from the database instead of JSON (so that it works offline as well and is faster).
My question is: how do I keep the database updated to make sure I have the same data in JSON and database?
What is the best approach to do this? I notice when I open the gmail app for example, it loads my emails even when I am offline, so it must be using a database as well.
EDIT: Solved it with using DBFlow and just calling save() on the whole list, which automatically decides whether to update a row or insert new one.

Scenario "DB is empty": Show empty view message and fetch data from API endpoint -> save that to database and afterwards populate RecyclerView from database.
Scenario "DB not empty": Populate from database -> try to fetch new data, if there is no connection it still shows old data otherwise do steps from first scenario.
Additionally you could compare the custom class that populates the RecyclerView to prevent glitches while reloading same data.

Related

How to save server data in your application data base and retreive it

I am making a portal sort of an app . I have used an API to receive update that I push in the listview. But with every new update I lose out on the news piece from previous response. Therefore I created a Database that stores the previous news items. Now the problem is that I get duplicate copies since the database stores the response again if the update from the API hasn't changed. I am a beginner in Android.
There are two way to solve your problem
This is easy solution when ever u receive json response before insert that data in to sqlite database u delete ur all data of sqlite database
then insert ur new response data in sqlite and call notifyDataSetChanged() methode to refresh the list.
This is something logical and depend upon ur json response. when ever u receive ur json response before insertion u check if data is available then update the data and if data is not available then insert the data.
u can check this condition through ur primary key or use any unique key for this condition.

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

Keeping a DB updated - Android

With your help, I have successfully created a web page which has its own MySQL DB and then uses a JSON web service to pass the values my Android application.
The next stage is to copy them into a local SQLite DB so the phone does not require an internet connection to view the list.
I have successfully implemented code that creates a table, and inserts values into this table.
The problem is, I don't want it to necessarily re-write the whole DB at a time, which is the current idea to keep it current.
I might have things in the DB deleted or more things added, and want the local DB to reflect this.
What steps can I take to delete things that are no longer present in the MySQL DB in the SQLite one, and add new things?
Cheers
The best way to do this should be to have a date time column in your database and then just fetch the posts that are newer then the last time you fetched from the database, in your android app you could save an date time in your preferences
I'm sorry I missed that you wanted to delete items as well, than this probably isn't the best approach. Are you using the mySQL only for the API? Then you could probably mark posts as deleted inserted odd feeling them.
You can think this approach, parse your JSON response into objects of your data model and then create the content values holding that data model to work with the database in your device.
Once you have the content values, use a content resolver to work with your database and try an update of each data parsed from the JSON response (each content value), update method of the content resolver will return an integer value that tell us the number of rows updated in the database, if such number is 0 means that no data were in the database, so make an insertion, simply.
In that way you first search if the current data is present in the database, if it is, update it, if not, insert it.

Android, Downloading Data and SQLite

I'm making an app which has to download some data, parse it and store it in a SQLite Database. However I'm having a problem where the downloadtask (an asynctask) executes everytime the app is launched and keeps appending the duplicate data to the database so I get multiple instances of the same data.
I only want to execute the download task if the database has values in it but still want to be able to run the downloadtask if the data file on the server is updated.
Other than checking if the number of rows in the table is greater than 0 how would I go about doing this? I'm not really sure what to search for. Any help would be appreciated!
Each reach should have a unique ID.
When loading the data check if the unique ID is in the database
If it exists update the row.
If it doesnt exist add it(append).
If you control the database in the website, you can put there a flag you update anytime the database is updated. So before reading the data, check your flag. If updated load new and append new data to the database.
For your local part of the app: Well you can easily update the data as you desires if a query on this database return more than one row. This is very easy using a cursor and getting its count after the query is executed. If you dont want to have duplicates just delete the database after you have made sure that your download task has retrieved the data from the remote source. In that way you won't have duplicates and you will be sure that you don't delete by accident the database if something goes wrong.
For the remote source:You can enable Push Notifications and send one after something is changed in the database.So by sending a notification you can trigger a service that will download the new data and parse it properly.

Caching Objects parsed from XML in 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.

Categories

Resources