java - sync between mysql and sql server for multiple users -


i developing android application. have used xampp server making use of mysql.when run app should sync mysql , retreive values , store in sqlite. tries example in link http://programmerguru.com/android-tutorial/how-to-sync-remote-mysql-db-to-sqlite-on-android/ . in same way developed code,but in example in link used has column called syncsts in mysql keep track of sync.but problem sync gets updated when 1 user uses app , status gets updated.again if other user uses app sync not happens.

my doubt is

  1. i want sync in such way when multiple user use app sync between mysql , sqlite should happen everytime , multiple users should access app.how modify code that.
  2. i using localhost here,problem runs emulator when tried real device doesnt work.only when pc , mobile in same network works.i want make multiple users use app different network.how do this.please help

my code is

mainactivity.java

public class mainactivity extends activity implements onclicklistener { textview update,updating; button btn1,btn2; hashmap<string, string> queryvalues; dbcontroller controller = new dbcontroller(this);   @override protected void oncreate(bundle savedinstancestate)  {     super.oncreate(savedinstancestate);     setcontentview(r.layout.activity_main);          asynchttpclient client = new asynchttpclient();      requestparams params = new requestparams();       client.post("http://192.168.1.104/website/getdbrowcount.php",params ,new asynchttpresponsehandler() {             @override             public void onsuccess(string response)              {                 system.out.println(response);                 try                 {                                      log.d("home", "success");                      // create json object out of response sent getdbrowcount.php                     jsonobject obj = new jsonobject(response);                     log.d("home", obj.tostring());                     system.out.println(obj.get("count"));                                // if count value not zero,                      if(obj.getint("count") != 0)                     {                         log.d("home", "count not equal zero");                         alertdialog.builder myalert=new alertdialog.builder(mainactivity.this);                         myalert.settitle("new product data available");                         log.d("home", "count");                         myalert.setmessage("new product data available.would download , update?");                         myalert.setpositivebutton("ok", new dialoginterface.onclicklistener()                          {                              @override                             public void onclick(dialoginterface dialog, int arg1)                             {                                 // todo auto-generated method stub                                  // transfer data remote mysql db sqlite on android , perform sync                                  syncdb();                                  update.settext("started syncing server");                                  btn2.setvisibility(view.visible);                             }                         });                            myalert.setnegativebutton("cancel", new dialoginterface.onclicklistener()                          {                              @override                             public void onclick(dialoginterface dialog, int arg1)                              {                                 // todo auto-generated method stub                                                                 update.settext("the update has been cancelled. please update via settings work"                                         + " latest sonetonix product data");                                  btn1.setenabled(true);                                  btn1.settextcolor(color.parsecolor("#ffffff"));                                  btn2.setvisibility(view.gone);                              }                         });                         myalert.show();                     }                     else                     {                          log.d("home", "count equal zero");                          update.settext("new products not available. please keep updating new products..");                         btn1.setenabled(true);                         btn1.settextcolor(color.parsecolor("#ffffff"));                         btn2.setvisibility(view.gone);                     }                   }                  catch (jsonexception e)                  {                 // todo auto-generated catch block                 e.printstacktrace();                 }              }              public void onfailure(int statuscode, throwable error,string     content)              {  }       });  }  public void  syncdb() {     log.d("home", "db sync");      // create asychttpclient object     asynchttpclient client = new asynchttpclient();  // http request params object     requestparams params = new requestparams();      client.post("http://192.168.1.104/website/getusers.php", params, new asynchttpresponsehandler()      {         @override        public void onsuccess(string response)          {              // update sqlite db response sent getusers.php             updatesqlite(response);         }            // when error occured         @override         public void onfailure(int statuscode, throwable error, string content)          {           }    });       }  public void  updatesqlite(string response) {      log.d("home",response);     arraylist<hashmap<string, string>> usersynclist;     usersynclist = new arraylist<hashmap<string, string>>();       // create gson object     gson gson = new gsonbuilder().create();     try     {         // extract json array response         jsonarray arr = new jsonarray(response);         system.out.println(arr.length());      // if no of array elements not 0         if(arr.length() != 0)         {                         (int = 0; < arr.length(); i++)              {                 // json object                 jsonobject obj = (jsonobject) arr.get(i);                 system.out.println(obj.get("productid"));                 system.out.println(obj.get("category"));                 system.out.println(obj.get("subcategory"));                 system.out.println(obj.get("mountingstyle"));                 system.out.println(obj.get("products"));                 system.out.println(obj.get("description"));               // db queryvalues object insert sqlite                 queryvalues = new hashmap<string, string>();                 queryvalues.put("productid", obj.get("productid").tostring());                 queryvalues.put("category", obj.get("category").tostring());                 queryvalues.put("subcategory", obj.get("subcategory").tostring());                 queryvalues.put("mountingstyle", obj.get("mountingstyle").tostring());                 queryvalues.put("products", obj.get("products").tostring());                 queryvalues.put("description", obj.get("description").tostring());                  // insert user sqlite db                 controller.insertuser(queryvalues);                  log.d("home","inserted properly");                  hashmap<string, string> map = new hashmap<string, string>();                 // add status each user in hashmap                 log.d("home",map.tostring());                  map.put("products", obj.get("products").tostring());                 map.put("status", "1");                  usersynclist.add(map);                  system.out.println("---------------------------------------------" + usersynclist);                  log.d("home",map.tostring());                 handler handler = new handler();                 handler.postdelayed(new runnable()                 {                       @override                       public void run()                        {                         //do after 100ms                        }                     }, 4000);             }           // inform remote mysql db completion of sync activity passing sync status of users             updatesyncsts(gson.tojson(usersynclist));           // reload main activity             reloadactivity();         }       }     catch (jsonexception e)     {         // todo auto-generated catch block         e.printstacktrace();     }  }   // method inform remote mysql db completion of sync activity public void updatesyncsts(string json) {       system.out.println(json);      asynchttpclient client = new asynchttpclient();      requestparams params = new requestparams();      params.put("syncsts", json);      system.out.println(params);      // make http call updatesyncsts.php json parameter has sync statuses of users         client.post("http://192.168.1.104/website/updatesyncsts.php", params, new asynchttpresponsehandler()         {             @override             public void onsuccess(string response)             {                 log.d("home",response);                 btn2.setvisibility(view.gone);                 btn1.setenabled(true);                 btn1.settextcolor(color.parsecolor("#ffffff"));             }                @override             public void onfailure(int statuscode, throwable error, string content)             {              }        });  }  // reload mainactivity public void reloadactivity() {     intent objintent = new intent(getapplicationcontext(), mainactivity.class);     startactivity(objintent);     update.settext("updated successfully"); }  } 

dbcontroller.java

public class dbcontroller extends sqliteopenhelper { private static final string database_name = "sonetonixproducts.db";  private static final int database_version = 1;   public dbcontroller(context context) {     super(context, database_name, null, database_version);     log.d("home",database_name);  }  @override  public void oncreate(sqlitedatabase database)  {      string query;     query="create table guide (slno integer primary key autoincrement, productid integer, category text, subcategory text, mountingstyle text, products text, description text )";     // todo auto-generated method stub      database.execsql(query);      log.d("home","table created"); }  @override public void onupgrade(sqlitedatabase database, int oldversion, int newversion) {     // todo auto-generated method stub     string query;     query= "drop table if exists guide";     database.execsql(query);     oncreate(database); }  public void insertuser(hashmap<string, string> queryvalues)       {         sqlitedatabase database = this.getwritabledatabase();         log.d("home",database.tostring());         contentvalues values = new contentvalues();          values.put("productid", queryvalues.get("productid"));         values.put("category", queryvalues.get("category"));         values.put("subcategory", queryvalues.get("subcategory"));         values.put("mountingstyle", queryvalues.get("mountingstyle"));         values.put("products", queryvalues.get("products"));         values.put("description", queryvalues.get("description"));         database.insert("guide", null, values);         database.close();          log.d("home","inserted");     }     public arraylist<string> getallusers()     {    arraylist<string> userslist;    userslist = new arraylist<string>();    string selectquery = "select category,subcategory guide";    sqlitedatabase database = this.getwritabledatabase();    cursor cursor = database.rawquery(selectquery, null);    log.d("home", cursor.tostring());    if (cursor.movetofirst()) {        {                                                                    userslist.add(cursor.getstring(0));          }while (cursor.movetonext());    }    database.close();    return userslist; }   public arraylist<hashmap<string, string>> getusers() {    arraylist<hashmap<string, string>> userslist;    userslist = new arraylist<hashmap<string, string>>();    string selectquery = "select category,subcategory guide";    sqlitedatabase database = this.getwritabledatabase();    cursor cursor = database.rawquery(selectquery, null);    if (cursor.movetofirst()) {        {            hashmap<string, string> map = new hashmap<string, string>();                         map.put(cursor.getstring(0), cursor.getstring(1));            userslist.add(map);         } while (cursor.movetonext());    }    database.close();    return userslist;  }  } 

how achieve this.please exaplanation

you can host web service , database on various free hosting websites https://www.2freehosting.com , after hosting can provide url of web service in android. can access database , web service on network on.

edit 1:

and issue or efficient sync of data application periodically check out : https://developer.android.com/training/sync-adapters/index.html


Comments

Popular posts from this blog

qt - Using float or double for own QML classes -

Create Outlook appointment via C# .Net -

ios - Swift Array Resetting Itself -