Inserting data into sqlite from server

I am trying to import some data from a server into my android app. Server gets data from sql server and returns its throw HttpRequest

. I am parsing data from HttpResponse

and storing it in sqlite database while parsing JSONObject

.

However, the number of objects inserted is large, and the operation can be canceled intentionally (by pressing pause) or unintentionally (Internet problem). Therefore, I have the following options:

1- Ignore the insert into the database after parsing JSONObject

and wait for a complete successful response from the server: this solution is very bad, because if a problem occurs, the user has to start importing data again.

2- Do feedback to the server when I insert a row into the database. Therefore, if I resume importing data, only new records are imported from the server: this is good, but imposes additional network connectivity and can also affect performance.

3. Get one data file and try to analyze it. So I am confident that pausing the operation will not result in data loss. But I prefer a different solution than working with a file.

What is the best way to deal with this problem?

thank

+3


source to share


1 answer


I went through this selection two weeks ago.

I found that the best way to manage this data is to create a web service on the server (for example in PHP, I used a Wordpress blog), send an offset and limit request to the web service, and then load the data in JSON format.

So if you have 100 rows to query, you can tell the web service to give you the first 10 results, then you can parse and save them, and then you can return with another query asking for the results from line 11 to row 20, etc.



This way, the application will manage many sequential requests (and corresponding junior JSON responses) rather than an extremely large JSON file.

You can also manage all of these activities in the background or with AsyncTask so that the user can work on the foreground activities at the same time.

0


source







All Articles