Android database synchronization with server database

I have an android application with two databases. I need to synchronize them with two identical (same field structures and field names) databases on a web server. But first, I need to look into another database on the web server, extract some information from it and synchronize these two databases with some additional data from the first database. I’ve been working on an Android application for some time now, and I just learned enough PHP with MySQL to build a web application with basic CRUD.

I have a couple of questions:

  • How do I sync these databases on Android?

  • Can synchronization work in both directions? If I change the database data on Android, can I change it accordingly on the web server and vice versa?

I am working on a basic GUI for seniors to use an Android phone. My web application should add contacts and reminders to the databases in the Android application, but when I add a contact or reminder manually in my Android application, I want to also see them in my web application (in both directions).

Thank!:)

+3
source share
4 answers

I think what you basically want to do is create an API to connect data with devices and data on your server.

API . GET POST, , API .

API, StackMob. ( ) API-, HTML5, iOS Android.

+2

, Android- PHP PHP, , - .., .

* → PHP: *

  • , : MyTableName: Login/Password
  • PHP

    <?php
    //To Connect to your Data base
      mysql_connect("localhost","root","");
      mysql_select_db("YourDataBaseName");
    // To execute a query from Your table
    
      $sql=mysql_query("SELECT *  FROM `Your_Table_Name");
    
      while($row=mysql_fetch_assoc($sql))
      $output[]=$row;
    
     // Convert to Json format 
     print(json_encode($output));
     // mysql_close();
    ?>
    
  • PHP , : MyTableName.php

  • : , Wamp www

  • , URL-, www.localhost/MyTableName.php, Json.

* → Android: *

  • 'ClientHTTP' https://www.dropbox.com/s/odpaus1tjhd9orv/ClientHTTP.java

  • .

  • put:

    String lReturn=_myClientHtpp.readFromUrl(www.localhost/MyTableName.php);
    // It will return a json format (Like navigator)
    //Code to Clear your data from Sqlite data base  TABLE ....
    
     try {
     // Create your Json Array
     JSONArray lList=new JSONArray(lReturn);
     // Iterate the json arry to get each Json object
     for (int i = 0; i < lList.length(); i++) {
    
    JSONObject lObject=lList.getJSONObject(i);
    
    String lLogin=lObject.getString("Login");
    String lPWD=lObject.getString("PassWord");
    
    //Code to Insert in Sqlite data base .....
    }
    } catch (JSONException e) {
     e.printStackTrace();
    }
    

* → PHP: *

<?php
//Connect to Database
  mysql_connect("localhost","root","");
  mysql_select_db("YourDataBase");


 $sql= mysql_query("INSERT INTO YourTableName (Login,PassWord) VALUES ('".$_REQUEST['PARAM_Login']."','".$_REQUEST['PARAM_PWD']."')");

 if($sql==1){
 echo "true";
 }else{
 echo "false";
 }

// mysql_close();
?>

* → Android: *

  • "" :

        // List of params
    ArrayList<NameValuePair> lListOfParams=new ArrayList<NameValuePair>();
    
    // Param login
    BasicNameValuePair PARAM_Login=new BasicNameValuePair("PARAM_Login", "test from Android");
    // Param PWD
    BasicNameValuePair PARAM_PWd=new BasicNameValuePair("PARAM_PWD", "Test from Android");
    
    // Add params to List
    lListParams.add(PARAM_Login);
    lListParams.add(PARAM_PWd);
    
    // Execute the post method from your ClientHttp class
    boolean lIsInsert=_myClientHtpp.SendToUrl("www.localhost/insert.php", lListOfParams);
    
    if(lIsInsert)
        Toast.makeText(this, "Insert Success", Toast.LENGTH_LONG).show();
    else
        Toast.makeText(this, "Insert Error", Toast.LENGTH_LONG).show();
    
  • , localhost .

  • , localhost IP-, , URL- : 192.168.1.2/MyTableName.php

+2

WebService, DataBase Android.

-: .

Android KSoap -. . .

0

- . SOAP android asp.net. , asp.net sql server 2008, Android SQLite. -. - SOAP

0

All Articles