Android Sync SQLite Database with Server using PHP and MySQL – Android Coding by DMTechnolab

The package The trap.Simplified coding.androidmysqlsync;

Import Android.Clearly;

Import Android.App.Progressressialog;

Import Android.Content.broadcast receiver;

Import Android.Content.Context;

Import Android.Content.Intent;

Import Android.Content.Intent filter;

Import the evening.package manager;

Import Android.The database.Cursor;

Import Android.The trap.Valency;


Import Android.Support.v7.App.AppCompatActivity;

Import Android.The.Bundle;

Import Android.Opinion.Opinion;

Import Android.Widget.switch;

Import Android.Widget.Change in writing;

Import Android.Widget.list View;

Import Android.Widget.Toast;

Import With.Android.Volley.Authorfailure;

Import With.Android.Volley.Request;

Import With.Android.Volley.request;

Import With.Android.Volley.Feedback;

Import With.Android.Volley.VolleyError;

Import With.Android.Volley.Toolbox.Stringering;

Import With.Android.Volley.Toolbox.Volley;

Import org.Jasan.JSONException;

Import org.Jasan.JSONObject;

Import Java.Useful.array list;

Import Java.Useful.hash map;

Import Java.Useful.The list;

Import Java.Useful.Map;

public class Main activity Is spread AppCompatActivity come into force Opinion.OnClickListener {{

/ *

* This is the URL for our webservice

* Make sure you are using IP instead of localhost

* If you are using localhost this will not work

* * /

public Fixed The last String URL_SAVE_NAME = = “”;

// database helper object

Private Database helper db;

// view items

Private switch buttonSave;

Private Change in writing editTextName;

Private list View List viewname;

// List to store all names

Private The list Name;

// 1 means data is synced and 0 means data is not synced

public Fixed The last int NAME_SYNCED_WITH_SERVER = Have 1;

public Fixed The last int NAME_NOT_SYNCED_WITH_SERVER = 0;

// Whether a broadcast data is synced to know the weather

public Fixed The last String DATA_SAVED_BROADCAST = = “net.simplifiedcoding.datasaved”;

// Broadcaster receiver to know sync status

Private broadcast receiver broadcast receiver;

// Adoptobiz for list view

Private NameAdapter nameAdapter;


Protected Zero Go ahead()Bundle saveInstanceState) {{

Super.Go ahead()saveInstanceState);


// initialize ideas and objects

db = = New Database helper()this);

Name = New array list());

buttonSave = ()switch) findViewById()R.Id.buttonSave);

editTextName = = ()Change in writing) findViewById()R.Id.editTextName);

List viewname = = ()list View) findViewById()R.Id.List viewname);

// add the click listener to the button


// Calling the method to load all stored names

Load name());

// broadcast receiver to update sync status

broadcast receiver = = New broadcast receiver()) {{


public Zero onReceive()Context The reference, Intent is intended) {{

// reload names

Load name());



// registering the broadcast receiver to update the sync status

Registrar()broadcast receiver, New Intent filter()DATA_SAVED_BROADCAST));


/ *

* This method will be

* Load name from database

* With update sync status

* * /

Private Zero Load name()) {{


Cursor Cursor = = db.getNames());

If ()Cursor.MoveToFirst())) {{

do {{

Name Name = = New Name()

Cursor.Get up()Cursor.getColumnIndex()Database helper.column name)),

Cursor.Tinted()Cursor.getColumnIndex()Database helper.COLUMN_STATUS))


Name.Add up()Name);

} whereas ()Cursor.MoveToNext()));


nameAdapter = = New NameAdapter()this, R.Layout.Name, Name);

List viewname.setAdapter()nameAdapter);


/ *

* This method will simply refresh the list

* * /

Private Zero refresh list()) {{



/ *

* Server is being named by this method

* * /

Private Zero saveNameToServer()) {{

The last Progressressialog PragatiDialogue = = New Progressressialog()this);

PragatiDialogue.Setmessage()“Saving Name …”);


The last String Name = = editTextName.Gettext()).Wire()).Trim());

Stringering Rigid = = New Stringering()Request.way.Post, URL_SAVE_NAME,

New Feedback.Listner()) {{


public Zero Treason()String Feedback) {{


try {{

JSONObject obj = = New JSONObject()Feedback);

If ()!obj.GetBoolean()“Error”)) {{

// if there is any success

// storing the name in sqlite to sync the position

saveNameToLocalStorage()Name, NAME_SYNCED_WITH_SERVER);

} other {{

// if there is some error

// Name the cyclite with the status as uncommented

saveNameToLocalStorage()Name, NAME_NOT_SYNCED_WITH_SERVER);


} Hold ()JSONException I) {{





New Feedback.Error maker()) {{


public Zero onErrorResponse()VolleyError Error) {{


// Storing the name in storelight to make the condition error-free

saveNameToLocalStorage()Name, NAME_NOT_SYNCED_WITH_SERVER);


}) {{


Protected Map<String, String> getParams()) Throws up Authorfailure {{

Map<String, String> Para = = New hash map());

The ultimate.Branch()“Name”, Name);

The return Para;



Volleysington.get Instance()this).addToRequestQueue()Rigid);


// name saving for local storage

Private Zero saveNameToLocalStorage()String Name, int Event) {{


db.addName()Name, Event);

Name N = = New Name()Name, Event);

Name.Add up()N);

refresh list());



public Zero on click()Opinion Opinion) {{




Source link

About dmtechnolab

Check Also

Simplified Coding

Android Hilt Tutorial – Injecting Dependencies with Hilt – Android Coding by DMTechnolab

You are welcome Android Hilt tutorial. I hope you all are aware of it Dependency …

Leave a Reply

Your email address will not be published. Required fields are marked *