0

我很难让我的 Android 应用程序将它收到的输入传输到 myphpadmin 服务器。

我想知道我需要在我的 Andriod 编码中添加什么,以便我的应用程序能够让 php 脚本运行并将信息传输到服务器。

到目前为止,我所做的是为配置和连接目的创建 php 脚本。

<?php
define('DB_USER', "root"); // db user
define('DB_PASSWORD', "password"); // db password (mention your db password here)
define('DB_DATABASE', "Wifi"); // database name
define('DB_SERVER', "http://andy-009.dlink.net/"); // db server
?>

/**
 * A class file to connect to database
 */
class DB_CONNECT {

    // constructor
    function __construct() {
        // connecting to database
        $this->connect();}
    // destructor
    function __destruct() {
        // closing db connection
        $this->close();}
  /**
     * Function to connect with database
     */
    function connect() {
        // import database connection variables
        require_once __DIR__ . '/db_config.php';

        // Connecting to mysql database
        $con = mysql_connect(DB_SERVER, DB_USER, DB_PASSWORD) or die(mysql_error());

        // Selecing database
        $db = mysql_select_db(DB_DATABASE) or die(mysql_error()) or die(mysql_error());

        // returing connection cursor
        return $con; }
    /**
     * Function to close db connection
     */
    function close() {
        // closing db connection
        mysql_close();
    } }

以及插入 php 脚本

update_product.php
<?php

/*
 * Following code will update a product information
 * A product is identified by product id (pid)
 */

// array for JSON response
$response = array();

// check for required fields
if (isset($_POST['WifiMacAddress']) && isset($_POST['WifiSSID']) && isset($_POST['WifiLatitude']) && isset($_POST['WifiLongtitude']) && isset($_POST['WifiLocation'])) {

    $WifiMacAddress = $_POST['WifiMacAddress'];
    $WifiSSID = $_POST['WifiSSID'];
    $WifiLatitude = $_POST['WifiLatitude'];
    $WifiLongtitude = $_POST['WifiLongtitude'];
    $WifiLocation = $_POST['WifiLocation'];

    // include db connect class
    require_once __DIR__ . '/db_connect.php';

    // connecting to db
    $db = new DB_CONNECT();

    // mysql update row with matched pid
    $result = mysql_query("UPDATE Wifi SET WifiSSID = '$WifiSSID', WifiLatitude = '$WifiLatitude', WifiLongtitude = '$WifiLongtitude' , WifiLocation = '$WifiLocation' WHERE WifiMacAddress = $WifiMacAddress");

    // check if row inserted or not
    if ($result) {
        // successfully updated
        $response["success"] = 1;
        $response["message"] = "Product successfully updated.";

        // echoing JSON response
        echo json_encode($response);
    } else {

    }
} else {
    // required field is missing
    $response["success"] = 0;
    $response["message"] = "Required field(s) is missing";

    // echoing JSON response
    echo json_encode($response);
}
?>
4

1 回答 1

0

我认为 android 和 php 之间的通信,JSON 会有所帮助。

您可以在两端将数据解析为 JSON 并使用 Web 服务通过 Internet 传输它们

于 2013-08-23T08:58:04.327 回答