0

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

rikojir
  • 115
  • 2
  • 13
  • 1
    check this answer http://stackoverflow.com/a/30026152/3843374 – Paritosh May 08 '15 at 13:32
  • php script is called? – Guillermo81 May 08 '15 at 13:42
  • Hey Paritosh, I checked your link, but I still have a question regarding those tutorials: I only POST things, I don't want to display things in my app. When I execute my httppost connecting to my php script, does the data I passed in my httppost get there in form of JSON and do I have to "unpack" that JSON data in my PHP script to get the original values? – rikojir May 08 '15 at 13:48
  • 1
    Yes, you are right. You will create key-value pairs on Android, and send it to you PHP API . On server, your code will receive these key-value pairs (just as you post-receive form data), and do whatever you want to do. – Paritosh May 08 '15 at 13:51
  • Okay, cool. So I guess I only have to change the two first lines in my php script. Since I'm a complete beginner in PHP, can you maybe tell me how I get those sent key-value-pairs "unpacked" and put into a variable? – rikojir May 08 '15 at 13:56
  • Check http://www.androidhive.info/2014/01/how-to-create-rest-api-for-android-app-using-php-slim-and-mysql-day-12-2/ and http://www.androidhive.info/2014/01/how-to-create-rest-api-for-android-app-using-php-slim-and-mysql-day-23/ link. This will explain you steps in detail. – Paritosh May 08 '15 at 14:10
  • `List params = new ArrayList(); params.add(new BasicNameValuePair("name", name));` Check here http://www.androidhive.info/2012/05/how-to-connect-android-with-php-mysql/ – Paritosh May 08 '15 at 14:43
  • This is exactly what I got in my android code, but that doesn't solve the problem. I don't know 2 things that have not been covered in the posted tutorials yet: 1) How to encode my data into JSON format in Android, then insert it using HttpClient on my database 2) How to unpack this JSON data in my php script, which has to be done, when step 1) is completed and the data is sent in JSON. – rikojir May 08 '15 at 21:10

0 Answers0