For at sende data til serveren kan du gøre dette:
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());
}
}
så for at sende lad os sige:
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);
}
så filen AddAccelerationData.php på serveren er:
<?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();
?>
Dette er et eksempel, jeg brugte for nylig. Bare at bemærke i php-filen. Jeg importerer Connection.phpthis handler kun om forbindelsen til databasen. Så bare udskift det med din kode for at oprette forbindelse til MYSQL db. Jeg importerede også SendAPI.php (som du bare kan ignorere) Dette var bare min klasse til at sende data. Grundlæggende indeholdt den nogle af de forespørgsler, jeg ville bruge. Såsom sendAccelerationData(). Grundlæggende svarede klassen til den for lagrede procedurer.