I have the following problem:
I have set up a database using XAMPP and I've written 4 PHP-Scripts to insert and show the content of it. That works fine by now. The database has two columns body and address both of type text and it is there to write some sms data in it.
Now I want to insert from my Android app. To achieve this, I have written those few lines of code inside my app:
//the sms to send
ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("body","testbody"));
nameValuePairs.add(new BasicNameValuePair("address", "testaddress"));
//http post
try{
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://10.0.2.2/sms/addsms.php");
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost);
}catch(Exception e){
Log.e("log_tag", "Error in http connection "+e.toString());
}
Now the problem is - if the code above has no fault - how can I pass those BasicNameValuePairs into my PHP variables? My PHP script for this looks like the following:
<?php
//This is my problem: How can I write the values from inside
//my android application in to those variables here? :(
//Getting the JSON data sent by the android app
$json_body = $_REQUEST['body'];
//Converting it into a standard class object
$obj_body = json_decode($json_body, true);
//Getting the value associated to the 'body' property
$body = $obj_body->'body';
//Getting the JSON data sent by the android app
$json_address = $_REQUEST['address'];
//Converting it into a standard class object
$obj_address = json_decode($json_address, true);
//Getting the value associated to the 'body' property
$address = $obj_address->'address';
//Connecting to database
require_once('mysqli_connect.php');
//Defining the query for inserting
$query = "INSERT INTO sms (body, address) VALUES (?,?)";
//Preparing the statement to be executed
$stmt = mysqli_prepare($dbc, $query);
//Binding the parameters to the statement
mysqli_stmt_bind_param($stmt, "ss", $body, $address);
//Executing the statement
mysqli_stmt_execute($stmt);
?>
I can run the app on the emulator, but nothing happens, so I get no new entry in my database. Can someone explain to me, how I get this right in PHP? Or is there a fault in the android code?
rikojir