Je veux synchroniser des données entre une application Android et un serveur mysql.

comment l'application fonctionne:

Lorsque l'application n'a pas Internet .. l'utilisateur entre les informations dans les champs et toutes les données seront enregistrées dans ma base de données sqlite.
et pour la prochaine fois que j'ai Internet je cliquera sur un bouton pour que toutes les données qui ont déjà été enregistrées dans ma base de données sqlite seront transmises à mon serveur mysql.

pouvez-vous me dire comment je peux le faire, ou partager avec moi un exemple qui fait cela? Ce serait vraiment bien si quelqu'un pouvait me montrer comment le faire ou me donner une bonne direction sur la façon de le faire.

je partagerai avec vous mon code,

DB_sqlite.java

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
 
import java.util.ArrayList;
 
 
public class DB_sqlite extends SQLiteOpenHelper
{
    //database version
    private static final int DB_VERSION = 1;
 
    //Constants for Database name, table name, and columns names
    public static final String DB_NAME = "data.db";
    public static final String TABLE_NAME = "contacts";
    public static final String COLUMN_ID = "id";
    public static final String COLUMN_NAME = "name";
    public static final String COLUMN_PRENOM = "prenom";
    public static final String COLUMN_GEO = "geoadress";
    public static final String COLUMN_CODE = "codeclient";
    public static final String COLUMN_RAISON = "raison";
    public static final String COLUMN_TEL = "tel";
    public static final String COLUMN_WTSP = "wtsp";
    public static final String COLUMN_OM = "Omatin";
    public static final String COLUMN_FM = "Fmatin";
    public static final String COLUMN_OAM = "OAmedi";
    public static final String COLUMN_FAM = "FAmedi";
    public static final String COLUMN_JV = "Jvisite";
    public static final String COLUMN_TP = "Tpose";
    public static final String COLUMN_SF = "superficie";
    public static final String COLUMN_EM = "emplacemet";
    public static final String COLUMN_CSP = "csp";
 
    public DB_sqlite(Context context) {
        super(context, DB_NAME, null, DB_VERSION);
    }
 
    @Override
    public void onCreate(SQLiteDatabase db)
    {
        String sql = "CREATE TABLE " + TABLE_NAME
                + "(" + COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, "
                + COLUMN_NAME + " VARCHAR, "
                + COLUMN_PRENOM + " VARCHAR,"
                + COLUMN_GEO + " VARCHAR,"
                + COLUMN_CODE + " VARCHAR,"
                + COLUMN_RAISON + " VARCHAR,"
                + COLUMN_TEL + " VARCHAR,"
                + COLUMN_WTSP + " VARCHAR,"
                + COLUMN_OM + " VARCHAR,"
                + COLUMN_FM + " VARCHAR,"
                + COLUMN_OAM + " VARCHAR,"
                + COLUMN_FAM + " VARCHAR,"
                + COLUMN_JV + " VARCHAR,"
                + COLUMN_TP + " VARCHAR,"
                + COLUMN_SF + " VARCHAR,"
                + COLUMN_EM + " VARCHAR,"
                + COLUMN_CSP + " VARCHAR) ";
 
        db.execSQL(sql);
 
    }
 
    @Override
    public void onUpgrade(SQLiteDatabase db, int OldVersion, int NewVersion)
    {
        Log.w("DBOpenHelper", "Mise à jour de la version " + OldVersion
                + " vers la version " + NewVersion
                + ", les anciennes données seront détruites ");
 
        String sql = "DROP TABLE IF EXISTS " + TABLE_NAME;
        db.execSQL(sql);
        onCreate(db);
    }
 
    public boolean insertData(String name,String prenom,String geoadress,String codeclient,String raison,String tel,String wtsp,String Omatin,String Fmatin,String OAmedi,String FAmedi,String Jvisite,String Tpose,String superficie,String emplacemet,String csp)
    {
        SQLiteDatabase db=this.getWritableDatabase();
        ContentValues contentValues = new ContentValues();
 
        contentValues.put("name",name);
        contentValues.put("prenom",prenom);
        contentValues.put("geoadress",geoadress);
        contentValues.put("codeclient",codeclient);
        contentValues.put("raison",raison);
        contentValues.put("tel",tel);
        contentValues.put("wtsp",wtsp);
        contentValues.put("Omatin",Omatin);
        contentValues.put("Fmatin",Fmatin);
        contentValues.put("OAmedi",OAmedi);
        contentValues.put("FAmedi",FAmedi);
        contentValues.put("Jvisite",Jvisite);
        contentValues.put("Tpose",Tpose);
        contentValues.put("superficie",superficie);
        contentValues.put("emplacemet",emplacemet);
        contentValues.put("csp",csp);
 
        long result = db.insert("contacts",null,contentValues);
 
        if (result==-1)
        {
            return false;
        }
        else
        {
            return true;
        }
 
    }
 
 
    public ArrayList getAllRecords()
    {
        ArrayList arrayList = new ArrayList();
        SQLiteDatabase db =  this.getReadableDatabase();
 
        Cursor res = db.rawQuery(" select * from "+ TABLE_NAME,null);
 
        //siirr l colomn lwl
        res.moveToFirst();
 
        while (res.isAfterLast()==false)
        {
            String t1 = res.getString(res.getColumnIndex(COLUMN_ID));
            String t2 = res.getString(res.getColumnIndex(COLUMN_NAME));
            String t3 = res.getString(res.getColumnIndex(COLUMN_PRENOM));
            String t4 = res.getString(res.getColumnIndex(COLUMN_GEO));
            String t5 = res.getString(res.getColumnIndex(COLUMN_CODE));
            String t6 = res.getString(res.getColumnIndex(COLUMN_RAISON));
            String t7 = res.getString(res.getColumnIndex(COLUMN_TEL));
            String t8 = res.getString(res.getColumnIndex(COLUMN_WTSP));
            String t9 = res.getString(res.getColumnIndex(COLUMN_OM));
            String t10 = res.getString(res.getColumnIndex(COLUMN_FM));
            String t11 = res.getString(res.getColumnIndex(COLUMN_OAM));
            String t12 = res.getString(res.getColumnIndex(COLUMN_FAM));
            String t13 = res.getString(res.getColumnIndex(COLUMN_JV));
            String t14 = res.getString(res.getColumnIndex(COLUMN_TP));
            String t15 = res.getString(res.getColumnIndex(COLUMN_SF));
            String t16 = res.getString(res.getColumnIndex(COLUMN_EM));
            String t17 = res.getString(res.getColumnIndex(COLUMN_CSP));
 
            arrayList.add(t1+"-"+t2+" "+t3+"/ADRESS ="+t4+"/CODE ="+t5+"/RAISON ="+t6+"/TEL ="+t7+"/WTSP ="+t8+"/OMATIN ="+t9+"/FMATIN ="+t10+"/OAMEDI ="+t11+"/FAMEDI ="+t12+"/JVISITE ="+t13+"/TPOSE ="+t14+"/SPRF ="+t15+"/EMPLCM ="+t16+"/CSP ="+t17);
            res.moveToNext();
        }
        return arrayList;
    }
 
    public Integer delete()
    {
        SQLiteDatabase db = this.getWritableDatabase();
 
        return db.delete(TABLE_NAME,null,null); //return 0 wlla 1
    }
 
 
}


MainActivity.java



Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
 
import java.util.ArrayList;
 
public class DB_sqlite extends SQLiteOpenHelper
{
    //database version
    private static final int DB_VERSION = 1;
 
    //Constants for Database name, table name, and columns names
    public static final String DB_NAME = "data.db";
    public static final String TABLE_NAME = "contacts";
    public static final String COLUMN_ID = "id";
    public static final String COLUMN_NAME = "name";
    public static final String COLUMN_PRENOM = "prenom";
    public static final String COLUMN_GEO = "geoadress";
    public static final String COLUMN_CODE = "codeclient";
    public static final String COLUMN_RAISON = "raison";
    public static final String COLUMN_TEL = "tel";
    public static final String COLUMN_WTSP = "wtsp";
    public static final String COLUMN_OM = "Omatin";
    public static final String COLUMN_FM = "Fmatin";
    public static final String COLUMN_OAM = "OAmedi";
    public static final String COLUMN_FAM = "FAmedi";
    public static final String COLUMN_JV = "Jvisite";
    public static final String COLUMN_TP = "Tpose";
    public static final String COLUMN_SF = "superficie";
    public static final String COLUMN_EM = "emplacemet";
    public static final String COLUMN_CSP = "csp";
 
    public DB_sqlite(Context context) {
        super(context, DB_NAME, null, DB_VERSION);
    }
 
    @Override
    public void onCreate(SQLiteDatabase db)
    {
        String sql = "CREATE TABLE " + TABLE_NAME
                + "(" + COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, "
                + COLUMN_NAME + " VARCHAR, "
                + COLUMN_PRENOM + " VARCHAR,"
                + COLUMN_GEO + " VARCHAR,"
                + COLUMN_CODE + " VARCHAR,"
                + COLUMN_RAISON + " VARCHAR,"
                + COLUMN_TEL + " VARCHAR,"
                + COLUMN_WTSP + " VARCHAR,"
                + COLUMN_OM + " VARCHAR,"
                + COLUMN_FM + " VARCHAR,"
                + COLUMN_OAM + " VARCHAR,"
                + COLUMN_FAM + " VARCHAR,"
                + COLUMN_JV + " VARCHAR,"
                + COLUMN_TP + " VARCHAR,"
                + COLUMN_SF + " VARCHAR,"
                + COLUMN_EM + " VARCHAR,"
                + COLUMN_CSP + " VARCHAR) ";
 
        db.execSQL(sql);
 
    }
 
    @Override
    public void onUpgrade(SQLiteDatabase db, int OldVersion, int NewVersion)
    {
        Log.w("DBOpenHelper", "Mise à jour de la version " + OldVersion
                + " vers la version " + NewVersion
                + ", les anciennes données seront détruites ");
 
        String sql = "DROP TABLE IF EXISTS " + TABLE_NAME;
        db.execSQL(sql);
        onCreate(db);
    }
 
    public boolean insertData(String name,String prenom,String geoadress,String codeclient,String raison,String tel,String wtsp,String Omatin,String Fmatin,String OAmedi,String FAmedi,String Jvisite,String Tpose,String superficie,String emplacemet,String csp)
    {
        SQLiteDatabase db=this.getWritableDatabase();
        ContentValues contentValues = new ContentValues();
 
        contentValues.put("name",name);
        contentValues.put("prenom",prenom);
        contentValues.put("geoadress",geoadress);
        contentValues.put("codeclient",codeclient);
        contentValues.put("raison",raison);
        contentValues.put("tel",tel);
        contentValues.put("wtsp",wtsp);
        contentValues.put("Omatin",Omatin);
        contentValues.put("Fmatin",Fmatin);
        contentValues.put("OAmedi",OAmedi);
        contentValues.put("FAmedi",FAmedi);
        contentValues.put("Jvisite",Jvisite);
        contentValues.put("Tpose",Tpose);
        contentValues.put("superficie",superficie);
        contentValues.put("emplacemet",emplacemet);
        contentValues.put("csp",csp);
 
        long result = db.insert("contacts",null,contentValues);
 
        if (result==-1)
        {
            return false;
        }
        else
        {
            return true;
        }
 
    }
 
 
    public ArrayList getAllRecords()
    {
        ArrayList arrayList = new ArrayList();
        SQLiteDatabase db =  this.getReadableDatabase();
 
        Cursor res = db.rawQuery(" select * from "+ TABLE_NAME,null);
 
        //siirr l colomn lwl
        res.moveToFirst();
 
        while (res.isAfterLast()==false)
        {
            String t1 = res.getString(res.getColumnIndex(COLUMN_ID));
            String t2 = res.getString(res.getColumnIndex(COLUMN_NAME));
            String t3 = res.getString(res.getColumnIndex(COLUMN_PRENOM));
            String t4 = res.getString(res.getColumnIndex(COLUMN_GEO));
            String t5 = res.getString(res.getColumnIndex(COLUMN_CODE));
            String t6 = res.getString(res.getColumnIndex(COLUMN_RAISON));
            String t7 = res.getString(res.getColumnIndex(COLUMN_TEL));
            String t8 = res.getString(res.getColumnIndex(COLUMN_WTSP));
            String t9 = res.getString(res.getColumnIndex(COLUMN_OM));
            String t10 = res.getString(res.getColumnIndex(COLUMN_FM));
            String t11 = res.getString(res.getColumnIndex(COLUMN_OAM));
            String t12 = res.getString(res.getColumnIndex(COLUMN_FAM));
            String t13 = res.getString(res.getColumnIndex(COLUMN_JV));
            String t14 = res.getString(res.getColumnIndex(COLUMN_TP));
            String t15 = res.getString(res.getColumnIndex(COLUMN_SF));
            String t16 = res.getString(res.getColumnIndex(COLUMN_EM));
            String t17 = res.getString(res.getColumnIndex(COLUMN_CSP));
 
            arrayList.add(t1+"-"+t2+" "+t3+"/ADRESS ="+t4+"/CODE ="+t5+"/RAISON ="+t6+"/TEL ="+t7+"/WTSP ="+t8+"/OMATIN ="+t9+"/FMATIN ="+t10+"/OAMEDI ="+t11+"/FAMEDI ="+t12+"/JVISITE ="+t13+"/TPOSE ="+t14+"/SPRF ="+t15+"/EMPLCM ="+t16+"/CSP ="+t17);
            res.moveToNext();
        }
        return arrayList;
    }
 
    public Integer delete()
    {
        SQLiteDatabase db = this.getWritableDatabase();
 
        return db.delete(TABLE_NAME,null,null); //return 0 wlla 1
    }
 
 
}

activity_main.xml

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
 
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.hp.myapplication8.MainActivity">
 
    <EditText
        android:id="@+id/editTextNom"
        android:layout_width="165dp"
        android:layout_height="40dp"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_alignParentTop="true"
        android:ems="10"
        android:hint="Nom"
        android:inputType="textPersonName" />
 
    <EditText
        android:id="@+id/editTextPrenom"
        android:layout_width="188dp"
        android:layout_height="40dp"
        android:layout_alignParentEnd="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:ems="10"
        android:hint="Prenom"
        android:inputType="textPersonName" />
 
 
        <ListView
            android:id="@+id/listdata"
            android:layout_width="match_parent"
            android:layout_height="60dp"
            android:layout_alignParentBottom="true"
            android:layout_below="@+id/btn_delete"
            />
 
    <Button
        android:id="@+id/btn_delete"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_below="@+id/spinner3"
        android:onClick="delete_data"
        android:text="Delete" />
 
    <Button
        android:id="@+id/btn_save"
        android:onClick="add_data"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentEnd="true"
        android:layout_alignParentRight="true"
        android:layout_alignTop="@+id/btn_delete"
        android:text="Save" />
 
    <EditText
        android:id="@+id/editTextGeo"
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_below="@+id/textView"
        android:ems="10"
        android:hint="GeoAdress"
        android:inputType="textPersonName" />
 
    <TextView
        android:id="@+id/textView"
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_below="@+id/editTextNom"
        android:text="Entrer : Ville &amp; Agence &amp; Secteur &amp; Quartier &amp; Voie  &amp; Nom route &amp; Num adress " />
 
    <EditText
        android:id="@+id/editTextCode"
        android:layout_width="165dp"
        android:layout_height="40dp"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_below="@+id/editTextGeo"
        android:ems="10"
        android:hint="CodeClient Proche"
        android:inputType="number" />
 
    <EditText
        android:id="@+id/editTextRaison"
        android:layout_width="188dp"
        android:layout_height="40dp"
        android:layout_alignBottom="@+id/editTextCode"
        android:layout_alignParentEnd="true"
        android:layout_alignParentRight="true"
        android:ems="10"
        android:hint="Raison"
        android:inputType="textPersonName" />
 
    <EditText
        android:id="@+id/editTextTel"
        android:layout_width="165dp"
        android:layout_height="40dp"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_below="@+id/editTextCode"
        android:ems="10"
        android:hint="Num Tel"
        android:inputType="phone" />
 
    <EditText
        android:id="@+id/editTextWtsp"
        android:layout_width="188dp"
        android:layout_height="40dp"
        android:layout_alignParentEnd="true"
        android:layout_alignParentRight="true"
        android:layout_below="@+id/editTextRaison"
        android:ems="10"
        android:hint="Num Wtsp"
        android:inputType="phone" />
 
    <EditText
        android:id="@+id/editTextOmatin"
        android:layout_width="165dp"
        android:layout_height="40dp"
        android:layout_alignEnd="@+id/editTextTel"
        android:layout_alignRight="@+id/editTextTel"
        android:layout_below="@+id/editTextTel"
        android:ems="10"
        android:hint="Ovrtr Matin"
        android:inputType="time" />
 
    <EditText
        android:id="@+id/editTextFmatin"
        android:layout_width="188dp"
        android:layout_height="40dp"
        android:layout_alignParentEnd="true"
        android:layout_alignParentRight="true"
        android:layout_below="@+id/editTextWtsp"
        android:ems="10"
        android:hint="frmtr matin"
        android:inputType="time" />
 
    <EditText
        android:id="@+id/editTextOAmedi"
        android:layout_width="165dp"
        android:layout_height="40dp"
        android:layout_alignEnd="@+id/editTextOmatin"
        android:layout_alignRight="@+id/editTextOmatin"
        android:layout_below="@+id/editTextOmatin"
        android:ems="10"
        android:hint="ovrtr apres medi"
        android:inputType="time" />
 
    <EditText
        android:id="@+id/editTextFAmedi"
        android:layout_width="188dp"
        android:layout_height="40dp"
        android:layout_alignParentEnd="true"
        android:layout_alignParentRight="true"
        android:layout_below="@+id/editTextFmatin"
        android:ems="10"
        android:hint="frmtr apres medi"
        android:inputType="time" />
 
    <EditText
        android:id="@+id/editTextJvisite"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_below="@+id/editTextOAmedi"
        android:ems="10"
        android:hint="Jour De Visite"
        android:inputType="textPersonName" />
 
    <Spinner
        android:id="@+id/spinner"
        android:layout_width="165dp"
        android:layout_height="40dp"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_below="@+id/editTextJvisite" />
 
    <Spinner
        android:id="@+id/spinner2"
        android:layout_width="188dp"
        android:layout_height="40dp"
        android:layout_alignParentEnd="true"
        android:layout_alignParentRight="true"
        android:layout_below="@+id/editTextJvisite" />
 
    <Spinner
        android:id="@+id/spinner3"
        android:layout_width="165dp"
        android:layout_height="40dp"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_below="@+id/spinner" />
 
    <Spinner
        android:id="@+id/spinner4"
        android:layout_width="188dp"
        android:layout_height="40dp"
        android:layout_alignParentEnd="true"
        android:layout_alignParentRight="true"
        android:layout_below="@+id/spinner2" />
</RelativeLayout>