Вставка в базу данных через Android

У меня есть следующая проблема:

Я создал базу данных с использованием XAMPP и написал 4 PHP-скрипта для вставки и отображения ее содержимого. Это прекрасно работает сейчас. База данных имеет два столбца тела и адреса обоих типов текста, и это там, чтобы написать некоторые данные смс в нее.

Теперь я хочу вставить из моего приложения для Android. Чтобы достичь этого, я написал несколько строк кода внутри моего приложения:

        //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());
        }

Теперь проблема в том, что, если в приведенном выше коде нет ошибок, как я могу передать эти BasicNameValuePairs в мои переменные PHP? Мой PHP-скрипт для этого выглядит следующим образом:

<?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);

?>

Я могу запустить приложение на эмуляторе, но ничего не происходит, поэтому я не получаю новую запись в своей базе данных. Может кто-нибудь объяснить мне, как я понимаю это правильно в PHP? Или есть ошибка в коде Android?

rikojir

0 ответов

Другие вопросы по тегам