How to save Parsed JSON for SQLite Android

I am making an Android application that receives data (text and images) from a JSON file on a server.

Everything is fine, I can parse the data and show it in my application.

Now I would like my app to be available offline (so 1 launch = load data, save to device)

I don't know how to start here. Where and how do I store my parsed JSON objects and how do I read them from the SQLite database on the device later and not from the server again?

I don't need the user to do input, just read from the server file and put it in a local DB on the device for offline use

Any suggestions please?

+3


source to share


2 answers


You can achieve this in two ways:

  • By saving json objects to a file and then saving the file to internal / external storage. in case of security, you should use internal storage to be stored in place: / data / data // files /

  • Saving a json object in the database, whose location will be / data / data // databases /. if your json object is large, you can use BLOB type for that column.



Hope this helps you.

0


source


Convert JSONObject to String and save as TEXT / VARCHAR. When retrieving the same column, convert String to JSONObject.

Example

Writing to the database:



String stringToBeInserted = jsonObject.toString();
//and insert this string into DB

      

Reading from the database:

String json = Read_column_value_logic_here 
JSONObject jsonObject = new JSONObject(json);

      

0


source







All Articles