Sync Resources





Just follow some steps for sending data from Android to MySQL:

  1. Create JSON object of your data using JSONObject and JSONArray class.
  2. post that as an string to your Web Service that might be in PHP or whatever you prefered.
  3. process to parse json and insert data in to MySQL database.

Same as from MySQL to Android:

  1. Create Web Service that displays your data in JSON Format.
  2. then you have to Request to that link and you’ll get JSON data, just you have to parse and store into SQLite.

You have to take care about Data Confiction. for that you have to take one that store some value both side likelast sync date/time.

There are some tutorial that will help for android JSON Parsing and PHP JSON Parsing.

  1. For Android
  2. For PHP

Facts to build own sync

If you write this yourself these are some of the points to keep in mind

  • Proper authentication between the device and the Sync Server
  • A sync protocol between the device and the server. It will usually go in 3 phases, authentication, data exchange, status exchange (which operations worked and which failed)
  • Pick your payload format. I suggest SyncML based XML mixed with JSON based format to represent the actual data. So SyncML for the protocol, and JSON for the actual data being exchanged
  • Keeping track of data changes on both client and server. You can maintain a changelog of ids that change and pick them up during a sync session. Also, clear the changelog as the objects are successfully synchronized
  • Need to have a way to communicate from the server to the device to start a sync session as data changes on the server. You can use C2DM or write your own persistent tcp based communication. The tcp approach is a lot seamless
  • A way to replicate data changes across multiple devices
  • And last but not the least, a way to detect and handle conflicts
  • To make data exchange, web server needs to provide RESTful web services (json, xml, etc) then mobile can receive data.


REST is 

Representational state transfer.  REST-style architectures conventionally consist of clients and servers.

RESTful web API HTTP methods

Resource 1:

PUT:Replace the entire collection with another collection.

POST: Create a new entry in the collection. The new entry’s URI is assigned automatically and is usually returned by the operation.

Resource 2:

PUT:Replace the addressed member of the collection, or if it doesn’t exist, createit.

POST:Not generally used. Treat the addressed member as a collection in its own right and create a new entry in it. 



Put vs Post (REST)

Both are used to create.



Used to modify and update a resource

POST /questions/<existing_question> HTTP/1.1

Note that the following is an error:

POST /questions/<new_question> HTTP/1.1

If the URL is not yet created, you should not be using POST to create it while specyfing the name. This should result in a ‘resource not found’ error because <new_question> does not exist yet. You should PUT the resource on the server first.

You could though do something like this to create a resources using POST:

POST /questions HTTP/1.1

Note that in this case the resource name is not specified, the new objects URL path would be returned to you.


Used to create a resource, or overwrite it. While you specify the resources new URL.

For a new resource:

PUT /questions/<new_question> HTTP/1.1

To overwrite an existing resource:

PUT /questions/<existing_question> HTTP/1.1