Android is the best way to sync SQLite with MySQL

I am working on a project containing a web application and a mobile application that records daily user data. The user can delete, update their data and use many devices to insert data.
I intend to develop this path:
The user enters their data and then inserts into SQLite. The service will be launched periodically (every 5 hours or sth) to synchronize with MySQL using a timestamp.
I searched for a sample using the service and timestamp on the Internet, but found nothing. It would be great if there was a sample or textbook.
I am new to Android and I have no idea how best to develop such an application. Thanks in advance.

---- EDIT ----
I consider using a timestamp or sync. Which is more efficient?

+6
source share
2 answers

you can use volley google library or any alternative to libraries , it depends on how you want to send data, the best approach is that you use JSON to make your life easier, get data from sqlite that you like to synchronize with your backend and send it over JsonObjectRequest using volley, for example, your request might look like this:

 jsonObjectRequest postForm = new JsonObjectRequest(Request.Method.POST, URL, YourJsonData, new Response.Listener<JSONObject>() { @Override public void onResponse(JSONObject response) { // here you can get your response. },new Response.ErrorListener() { @Override public void onErrorResponse(VolleyError error) { // here you can tell there is something went wrong. }); 

u can add a new value that indicates whether the value was synchronized or not in your local database. for example, let's say you have a table with a student, and in this table there are three columns that are ID, NAME and synchronized in the above code, when your answer returns a successful update of this column with the synchronized column with true | false, which indicates whether this line will be synchronized with your backend or not. and in order to get data from your database you have to do something like this.

 public String getStudents() { List<Student> students = new ArrayList<Student>(); String query = "SELECT * FROM " + STUDENT+ "where synced=0"; SQLiteDatabase db = this.getWritableDatabase(); Cursor cursor = db.rawQuery(query, null); if (cursor.moveToFirst()) { do { Student st = new Student(); st.setId(cursor.getString(cursor.getColumnIndex(ID))); st.setName(cursor.getString(cursor.getColumnIndex(NAME))); st.setSynced(cursor.getInt(cursor.getColumnIndex(SYNCED))); students.add(st); } while (cursor.moveToNext()); } db.close(); return new Gson().toJson(students); } 
+5
source

An easy way for AFAIK is to use lib, for example http://loopj.com/android-async-http/ or volley lije k0sh says to send data to a PHP script that will manage your mysql data. You will need to write a php (or java) script on your server to receive your data (you must write a REST API)

To select your HTTP file, you should look here:

What is the most reliable HTTP library for Android?

You really need to take care of how you are going to sync your data, because it can drain your battery. Here you will learn how to optimize your updates:

https://developer.android.com/training/efficient-downloads/index.html

Hope this helps!

+5
source

Source: https://habr.com/ru/post/977623/


All Articles