PHP – send data from Android to the server via JSON
>I have MySQL database server > I need to update and retrieve the data of MySQL server through JSON
Therefore, I want to know how to do this. I am very confused with this example statement:
HttpPost httppost = new HttpPost("http://10.0.2.2/city.PHP");
This means that I need to write an application to connect to the PHP server before writing PHP to connect to the database, right?
resolvent:
To send data to the server, you can do this:
private void sendData(ArrayList<NameValuePair> data)
{
// 1) Connect via HTTP. 2) Encode data. 3) Send data.
try
{
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new
HttpPost("http://www.blah.com/AddAccelerationData.PHP");
httppost.setEntity(new UrlEncodedFormEntity(data));
HttpResponse response = httpclient.execute(httppost);
Log.i("postData", response.getStatusLine().toString());
//Could do something better with response.
}
catch(Exception e)
{
Log.e("log_tag", "Error: "+e.toString());
}
}
Then send, let's say:
private void sendAccelerationData(String userIDArg, String dateArg, String timeArg,
String timeStamp, String accelX, String accelY, String accelZ)
{
fileName = "AddAccelerationData.PHP";
//Add data to be send.
ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(7);
nameValuePairs.add(new BasicNameValuePair("userID", userIDArg));
nameValuePairs.add(new BasicNameValuePair("date",dateArg));
nameValuePairs.add(new BasicNameValuePair("time",timeArg));
nameValuePairs.add(new BasicNameValuePair("timeStamp",timeStamp));
nameValuePairs.add(new BasicNameValuePair("accelX",accelX));
nameValuePairs.add(new BasicNameValuePair("accelY",accelY));
nameValuePairs.add(new BasicNameValuePair("accelZ",accelZ));
this.sendData(nameValuePairs);
}
The addaccelerationdata.php file on the server is:
<?PHP
/*
* What this file does is it:
* 1) Creates connection to database.
* 2) Retrieve the data being send.
* 3) Add the retrieved data to database 'Data'.
* 4) Close database connection.
*/
require_once '../Connection.PHP'; //connect to a database/disconnect handler.
require_once '../SendAPI.PHP'; //deals with sending querys.
$server = new Connection();
$send = new Send();
//Connect to database.
$server->connectDB();
//Retrieve the data.
$userID = $_POST['userID'];
$date = $_POST['date'];
$time = $_POST['time'];
$accelX = $_POST['accelX'];
$accelY = $_POST['accelY'];
$accelZ = $_POST['accelZ'];
//Add data to database 'Data'. //Personal method to query and add to database.
$send->sendAccelerationData($userID, $date, $time, $timeStamp, $accelX, $accelY, $accelZ);
//Disconnect from database.
$server->disconnectDB();
?>
This is an example I recently used. Just note in the PHP file. I import connection.php, which only handles the connection with the database. So I just need your code to replace it to connect to MySQL dB. I also import sendapi.php (you can ignore) which is just the class I send data. Basically, it contains some queries I want to use. Such as sendaccelerationdata() Classes are basically similar to stored procedures