In my Android application I have passed the sqlite data into remote mysql database using JSON over AsyncHttp protocol.
Now I want the data from Mysql database to SQLite database so that from server I am fetching the data and converting it to JSON.
Since server has many rows of data I need to fetch only my mobiles sqlite data so that I need to pass the variables and check if the data is mine or not rather than fetching all the users data.
How to pass the variables in the url so that while fetching only I want to search my users data and insert them to SQLITE database.
Here is my code
public void syncMySQLDBSQLite(){
// Create AsycHttpClient object
AsyncHttpClient client = new AsyncHttpClient();
// Http Request Params Object
RequestParams params = new RequestParams();
// Show ProgressBar
prgDialog.show();
// Make Http call to getusers.php
client.post("http://10.0.2.2/tafapps/getuser.php", params, new AsyncHttpResponseHandler() {
@Override
public void onSuccess(String response) {
// Hide ProgressBar
prgDialog.hide();
// Update SQLite DB with response sent by getusers.php
updateSQLite(response);
}
// When error occured
@Override
public void onFailure(int statusCode, Throwable error, String content) {
// TODO Auto-generated method stub
// Hide ProgressBar
prgDialog.hide();
if (statusCode == 404) {
Toast.makeText(getApplicationContext(), "Requested resource not found", Toast.LENGTH_LONG).show();
} else if (statusCode == 500) {
Toast.makeText(getApplicationContext(), "Something went wrong at server end", Toast.LENGTH_LONG).show();
} else {
Toast.makeText(getApplicationContext(), "Unexpected Error occcured! [Most common Error: Device might not be connected to Internet]",
Toast.LENGTH_LONG).show();
}
}
});
}
public void updateSQLite(String response){
ArrayList<HashMap<String, String>> usersynclist;
usersynclist = new ArrayList<HashMap<String, String>>();
// Create GSON object
Gson gson = new GsonBuilder().create();
try {
// Extract JSON array from the response
JSONArray arr = new JSONArray(response);
Toast.makeText(getApplicationContext(), response, 5000).show();
System.out.println(arr.length());
// If no of array elements is not zero
if(arr.length() != 0){
// Loop through each array element, get JSON object which has userid and username
for (int i = 0; i < arr.length(); i++) {
// Get JSON object
JSONObject obj = (JSONObject) arr.get(i);
System.out.println(obj.get("userId"));
System.out.println(obj.get("userName"));
db = this.openOrCreateDatabase("Hangman", MODE_PRIVATE, null);
// String id = obj.get("userId").toString();
name = obj.get("userName").toString();
email = obj.get("userEmail").toString();
phn = obj.get("userPhnum").toString();
plyd = obj.get("userPlayed").toString();
crct = obj.get("userCorrect").toString();
//Toast.makeText(getApplicationContext(), email, 5000).show();
db.execSQL("insert into scores(userName,userEmail,userPhnum,userPlayed,userCorrect)values('"+name+"','"+email+"','"+phn+"','"+plyd+"','"+crct+"')");
}
//Toast.makeText(getApplicationContext(), ""+queryValues, 5000).show();
// Inform Remote MySQL DB about the completion of Sync activity by passing Sync status of Users
updateMySQLSyncSts(gson.toJson(usersynclist));
}
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}