Standard way to sync sqlite data to online server using webservice - android

I have local sqlite storage for my app. Once the user enables online sync option the data syncs with my online server using Asp.Net webservice. For syncing i am concatenating all tables' data in a single string and then passing that string parameter to service.
Example : "table_name1$col1_value$col2_value$col3_value|table_name2$col21_value$col22_value$col23_value.."
Its really hard to manage sync status of each row using this approach. And somehow it doesnt looks like a good approach to me.
The other approach i tried and looks better than this is to pass List of tablename1, List of tablename2 objects in a single containg object or as different parameters.
I am sending data in JSON format.
What approach should i follow to pass the data to webservice(any better approach than these two). A optimal and standardized approach for this.

Best approach is use a time stamp to handle this.
Initial request to server with time stamp value 0.
Server will give the all the data first time with Time-stamp.
Store the Time stamp to sharedpreferences.
In All next request pass the time stamp back to the server
Server will send only those data which are add/update/ after that
given time stamp
That is it.

Related

Sync data between client and server

I have mobile app. Something like to do list or calendar. Teoretically user can have a few devices with that application on a defferent platforms and so on. I would like to create a automatic synchronization between them through a own server. What is the best practice: update all the information or only the changes? On the one hand usually there is no a lot of data when it's about a to do list but who knows?
The correct approach is not date/time as others suggest, as time can go out of sync. The right algorithm is to keep the checksum of the data entries during last synchronization. On next synchronization you compare current checksums with stored ones, then you know whether the entry has been changed on the server, on the client or both.
Our open-source Rethync SDK lets you implement the above approach quite easily and is available for Android (not for iOS at the moment).
I am doing something similar in my application. I have a last modified date field with each entity that I need to sync. Then periodically, I post this data to the server (actual data + date and time). Now the server can do one of two things. It will check the corresponding data on server side and compare the last modified date. If what the server is latest, it will return the latest data in response. If not, it will update its data and send a response indicating what client has is latest.
Of course you can do several optimization. That is, mark the data as "dirty" so you know whether to even send your data to server. If the phone does not have modified data, your sync is basically getting the latest data from server.
Basically server does the heavy lifting and does all the logic necessary to maintain the latest data on its end and send responses to client appropriately.
Good Luck
Best approach is use a time stamp to handle this.
Initial request to server with time stamp value 0.
Server will give the all the data first time with Time-stamp.
Store the Time stamp to sharedpreferences.
In All next request pass the time stamp back to the server
Server will send only those data which are add/update/ after that
given time stamp
That is it.
There is a new alternative to the syncing problem. It's called EnduroSync from Orando Labs. You can sync object data stores between devices on Android and iOS now, with others coming soon.
Full Disclosure: I work for Orando Labs.
The EnduroSync clients allow you to create object data stores on the local devices. The clients are fairly sophisticated - data is modeled as native objects for each client we support (iOS and Android now, more coming). The clients work offline and online. The data is saved to an sqlite database locally.
As you change objects in your model, the deltas are recorded on the device. At some point, you can 'sync' the object data store. Syncing uses a commit/push/pull process (like git), but this is invisible to you. The sync brings your local copy up to date with whatever is on the server, and sends up any changes you have made. Conflicts are resolved using a timestamp based merge, so newer data is not overwritten by older data.
EnduroSync is an online service, so there is no server setup on your end.
There is also a flexible permission system which lets you share the object data stores in a variety of ways. For instance, most applications will have one or more object data stores for each user, for preferences, notes, tags, etc. You can also share object data stores per app, per user type, and with wild cards, many other ways.
So basically you use our client SDK's to model your data on the device. Modeling is with simple objects in the native programming language of the device. If you sign up for the syncing service, you get the syncing also.
Here is another approach.
Issue :I need to have the appointments of doctors syned to client (mobile device) from the server. Now the appointments can drop off or the data could possibly change on the server. Having the client to know what change and sending a request back to server could be an expensive service.
Possible approach : Have the server do the heavy lifting. Keep a table which stores values of time stamp and if a change happened with regard to an appointment - cancellation / reschedule etc. The client would then look at this table to see if anything changed. In reality we don't need to sync anything but only the delta which server can provide to the client based on what it has and what is at Client. There is one aspect which needs to be taken care of is updation of info from client to server and traditional conflict management can be done where client can update the server when a data connectivity between client and server exists.
Essentially the approach is to have only the deltas synced by maintaining a checksum or data change log to PUSH changes to the client.

Best Practice for loading data from server (Android App)

I'm building an Android app, which should:
show some data, loaded from a server in the Internet.
At the moment I have a local SQliteDB used in my app where the data is stored, which should be displayed. I use this, because I want to be able to show the data, even if there is temporarily no internet connection available.
Next step I will work on inserting data in the local SQliteDB from a internet server. I thought about doing it this way:
When app starts, check if internet is available. If yes, connect to a webservice (including username and password). The webservice should deliver the necessary data via json object to the app and I will update the local SQlite DB.
My questions:
Is this a good idea?
Are there any better ways to do this?
The data can be viewed (and edited) by a Zend Website, too.
Thanks in advance.
Best regards
Daniel
The way you put it seems optimal. Maybe you should set a flag or alert which is time or date related..in case the app starts too many times without internet.
>> For updates to your mobile app, you should consider the priority/urgency of having the same data on the server and your app.
> For the better ways to do it, you can opt the way which suits your requirement better.
To fetch the data in one thread and render it in another,
1. Write custom Asynctasks:
http://developer.android.com/reference/android/os/AsyncTask.html
AsyncTask Android example
OR
2. Use something like AsyncHttpClient: http://loopj.com/android-async-http/
where you get onSuccess and onFailure methods to work with the response.
The option 2. is better if you just want to fetch data without doing anything else, and work on it, or save it. For the same, you need to parse the response first.
To parse your data:
As your response is JSON format, you may be better off using Gson to map data and with custom model classes. Eg.:
Gson gson = new Gson();
ModelClass modelClass= new ModelClass();
modelClass= gson.fromJson(responseContent,ModelClass.class);
//where responseContent is your jsonString
Log.i("Web service response", ""+modelClass.toString());
More on: https://code.google.com/p/google-gson/
For Naming discrepancies(according to the variables in webservice), can use annotations like
#SerializedName.
Use a for each loop to verify/browse/access the data that would be populated in/as objects/fields of your model class:
Check these for doubts:
http://docs.oracle.com/javase/1.5.0/docs/guide/language/foreach.html
How does the Java 'for each' loop work?
Now about saving your data:
>> It depends a lot on what the data from server is and how much data do you want to store.
In Android Storage Options:
http://developer.android.com/guide/topics/data/data-storage.html
a. There's Shared Preferences:
These are good for saving/storing data which would be relatively small in size and could be overwritten and fetched frequently. Eg. username, current user's details, password
http://developer.android.com/reference/android/content/SharedPreferences.html
How to use SharedPreferences in Android to store, fetch and edit values
b. Maintaining a database is good for the larger chunk needed in your app.
You can store, update or over-write the data according to your need. There can be multiple tables or more data could be stored in various fields.
http://developer.android.com/reference/android/database/sqlite/SQLiteDatabase.html
http://www.vogella.com/tutorials/AndroidSQLite/article.html
While you use Gson, you also have the option of populating the objects of model class and then storing that response in a String(maybe in SharedPreferences, depending on length/size) using gsonToJson method. I have used that, cheeky but effective.
You need to consider other stuff too, pertaining to UI and memory optimization, ListViews or layouts etc depending on your app and its control flow.
You could start a thread and get new data on loading the app. If you decide this path we had nice results with JSON using the Volley Project.
You can see a walk through here http://www.javacodegeeks.com/2013/06/android-volley-library-example.html
and get the code here
https://android.googlesource.com/platform/frameworks/volley/
Try using this library:-
https://github.com/Abhishekpalodath/FetchServerUtil-Android.git.
You can also load real-time data from the server by using this.

Android data best practices

I am building an Android app that that involves taking pictures and adding text to it. In other apps I have built I have been storing the data locally as JSON data. I read that you should always save locally(JSON) at every step because you can never be sure when an activity will be stop.
In the app I am currently building I want to upload the data to a SQL database. For example want to take a picture, save it locally and edit it(while saving at every edit), and then upload it to server. Then I would want to display a list that accesses the database to display finished photos.
Can I continue to save things with JSON and then only upload certain things to the database or should I skip JSON and just go directly to database?
Is there any built in methods for sending JSON data to a SQL database? Just trying to figure out the best way to go about this. Any advice would be appreciated.
I would not store the data as JSON because you might need later to query the data or filter it which will be cumbersome to do it with JSON.
What I suggest that you store your data in sqlite (the text only) and then when you are about to send to the server you can serialize it as JSON.
There is no special method to send JSON data, it is just normal plain text over HTTP.
As general rule, data storage should be in a database and then you can use JSON (or XML) to exchange the data (e.g. sync it with the server)
Also, if you are sending large amount of data, consider compressing it before sending to the server

How to speed up synchronizing json data from the server using json parser in android?

Sync data from the server using json parser
I have developed android application for offline concept.When internet connected more than 4000 records synchronized from the server. If i add only one data in the server. when i do second time synchronizing, that particular data to be synchronize from the server.My problem is when i do second time synchronizing data it took same time as first time synchronizing. how to avoid this,please advice me.
Synchronizing working fine. But if i press the sync button second time. whole data can be synchronizing again. Please explain in detail. I have little bit idea i don't know whether it is correct or not.
My suggestion:
Flag to be set in the server side and client side. If data synchronized flag to be set as 0. if data newly synchronized it should be set as 1.
if data loss. unfortunatly data cache cleared in application. how to do this?. please explain me.
Check this video
Here a googler explain how to build REST applications - there is a lot of usefull thing about sync.
If you build server and client by youw own - you can store timestamp of the last connection and each time you synchronize you pass this time to the server and depend on it you can pass only modified columns (you have to store timeStamp of modification for data or list of modified columns from key timestamps)

Store data for few minutes for further access

I have a done a project in which data like name, url, desc are coming from server using web-service. Every time I need to parse it and then display. I want to use store parsed data in a particular are in which parsed data will be removed after five minutes. Is this possible to do that, if yes then how? I have searched lot and found one thing "mamcache" but not clear how can be used mamcache in Android
Edit
Actually in my app I have different category and clicking on every category, sending request to the server and then parsing it and then displaying. I don't want to send request every time, so I think we should use some technique by which we download data when app runs first time and then store parsed data in a temporary location (this location will store data only for five minutes). I will check often if temporary location has data then fetch from there otherwise send request download, parse and then display.
Your description of what you need is a little vague, but if you need to persist parsed data you can build a model for your data to store it temporarily, or you can save the data to the application preferences which will be available across app instances.
If you can better describe exactly how you want to use the parsed data, I can give you explicit direction?
Edit
If you only need to persist data for a short period of time within the same activity, then use variables to store your parsed data. If the variables are populated, then don't hit the server. If the variables have not been initialized, then hit the server, parse and put the data into the respective variables. As for holding the data for 5 minutes, use a datetime variable to check if the other variable data is stale.
Do you have control over the server? You could implement caching.
See blog post here:
http://android-developers.blogspot.com/2011/09/androids-http-clients.html
HTTP Caching has been back ported here:
http://code.google.com/p/httpclientandroidlib/
You could also implement a cache yourself using DiskLruCache.

Categories

Resources