I am developing an android app that is connected to a Mysql database through a php webservice. Right now I can read the data from the Mysql database and add them to my android sqlite database just fine.
Now I need to push any updates to the data from the database to the app. After looking into this a lot the best solution that is suggested is GCM however due to some restrictions in the project I am not allowed to use it. Can anyone suggest any alternatives keeping in mind that I am fairly new at all of this.
Thanks.
AsyncTask is an abstract class provided by Android which helps us to use the UI thread properly. This class allows us to perform long/background operations and show its result on the UI thread without having to manipulate threads.
You can use AsyncTask to call your webservices:
private class LongOperation extends AsyncTask<String, Void, String> {
@Override
protected String doInBackground(String... params) {
try {
//call your webservice to perform MySQL database opration
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder()
.permitAll().build();
StrictMode.setThreadPolicy(policy);
HttpClient httpclient = new DefaultHttpClient();
HttpGet httpget = new Http Get("http://yourserver.com/webservices/service.php?id="
+ URLEncoder.encode("record_id") +"¶m1="
+ URLEncoder.encode("param1 value") + "¶m2="+ URLEncoder.encode("param2 value"));
HttpResponse response = httpclient.execute(httpget);
final String str=EntityUtils.toString(response.getEntity());
myjson = new JSONObject(str);
//perform JSON parsing to get webservice result.
if (myjson.has("success") == true) {
//Updation is succesful
} else {
//failed to perform updation
}
} catch (InterruptedException e) {
e.printStackTrace();
}
return "Executed";
}
@Override
protected void onPostExecute(String result) {
// This will be executed after completion of webservice call. and `String result` will have returned value from doInBackground()
// might want to change "executed" for the returned string passed
// into onPostExecute() but that is upto you
}
@Override
protected void onPreExecute() {}
@Override
protected void onProgressUpdate(Void... values) {}
}
now, perform webservice call by creating object of LongOperation
class,
LongOperation webCall = new LongOperation();
webCall.execute();
in PHP you should wrote as follows:
<?php
//DB Connection code:
$dbhost = "server";
$dbuser = "user_name";
$dbpassword = "pass";
$database = "your_db";
// connect to the database
$db = mysql_connect($dbhost, $dbuser, $dbpassword) or die("Connection Error: ".mysql_error());
mysql_select_db($database, $db) or die("Error conecting to db.");
header("Content-type: text/json");
if (!isset($_GET['id']) || $_GET['id'] == "" ||!isset($_GET['param1']) || $_GET['param1'] == "" || !isset($_GET['param2']) || $_GET['param2'] == "" ){
echo json_encode(array('error' => 'Required arguments missing.'));
exit;
}
$id = mysql_real_escape_string($_GET['id']); //escape string to prevent SQL injection attack.
$param1 = mysql_real_escape_string($_GET['param1']);
$param2 = mysql_real_escape_string($_GET['param2']);
$sql = "update your_table set param1='$param1',param2='$param2' where id=$id";
mysql_query($sql);
if (mysql_affected_rows()==1) {
echo json_encode(array('success' => "updated"));
}else{
echo json_encode(array('error' => "not updated"));
}
?>
You can use POST method to pass parameters to webservice to make it more secure. :)