使用php 1将Android与MySQL数据库连接时,eclipse logcat中出现错误

使用php 1将Android与MySQL数据库连接时,eclipse logcat中出现错误,android,android-emulator,Android,Android Emulator,我下载了代码,并尝试在emulator中执行项目。我的应用程序是在emulator中创建的,我可以执行mainscreenactivity,但无法移动到下一个活动来执行CRUD操作。在emulator中,加载后,不幸的是, 主屏幕已作为弹出消息停止。我的应用程序名是MainScreen。我检查了EclipseLogcat,它显示了以下错误。如果有人能帮我解决这个问题,我将不胜感激 日志: 12-12 07:41:01.496:E/ActivityThread685:android.app.Ser

我下载了代码,并尝试在emulator中执行项目。我的应用程序是在emulator中创建的,我可以执行mainscreenactivity,但无法移动到下一个活动来执行CRUD操作。在emulator中,加载后,不幸的是, 主屏幕已作为弹出消息停止。我的应用程序名是MainScreen。我检查了EclipseLogcat,它显示了以下错误。如果有人能帮我解决这个问题,我将不胜感激

日志:

12-12 07:41:01.496:E/ActivityThread685:android.app.ServiceConnection泄漏:Service com.android.exchange.ExchangeService已泄漏ServiceConnection com.android.emailcommon.Service.ServiceProxy$ProxyConnection@40d5daa8原来是订在这里的 12-12 07:41:01.496:E/ActivityThread685:at-android.app.LoadedApk$ServiceDispatcher.LoadedApk.java:969 12-12 07:41:01.496:E/ActivityThread685:at-android.app.LoadedApk.getServiceDispatcherLoadedApk.java:863 12-12 07:41:01.496:E/ActivityThread685:at-android.app.ContextImpl.bindServiceContextImpl.java:1418 12-12 07:41:01.496:E/ActivityThread685:at-android.app.ContextImpl.bindServiceContextImpl.java:1407 12-12 07:41:01.496:E/ActivityThread685:at-android.content.ContextWrapper.bindServiceContextWrapper.java:473 12-12 07:41:01.496:E/ActivityThread685:at com.android.emailcommon.service.ServiceProxy.setTaskServiceProxy.java:157 12-12 07:41:01.496:E/ActivityThread685:at com.android.emailcommon.service.ServiceProxy.setTaskServiceProxy.java:145 12-12 07:41:01.496:E/ActivityThread685:com.android.emailcommon.service.ServiceProxy.testServiceProxy.java:191 12-12 07:41:01.496:E/ActivityThread685:com.android.exchange.ExchangeService$7.runExchangeService.java:1850 12-12 07:41:01.496:E/ActivityThread685:com.android.emailcommon.utility.utility$2.doInBackgroundUtility.java:551 12-12 07:41:01.496:E/ActivityThread685:com.android.emailcommon.utility.utility$2.doInBackgroundUtility.java:549 12-12 07:41:01.496:E/ActivityThread685:at android.os.AsyncTask$2.callAsyncTask.java:287 12-12 07:41:01.496:E/ActivityThread685:at java.util.concurrent.FutureTask.runFutureTask.java:234 12-12 07:41:01.496:E/ActivityThread685:at java.util.concurrent.ThreadPoolExecutor.runWorkerThreadPoolExecutor.java:1080 12-12 07:41:01.496:E/ActivityThread685:at java.util.concurrent.ThreadPoolExecutor$Worker.runThreadPoolExecutor.java:573 12-12 07:41:01.496:E/ActivityThread685:at java.lang.Thread.runThread.java:856 12-12 07:41:01.526:E/StrictMode685:null 12-12 07:41:01.526:E/StrictMode685:android.app.ServiceConnection泄漏:Service com.android.exchange.ExchangeService已泄漏ServiceConnection com.android.emailcommon.Service.ServiceProxy$ProxyConnection@40d5daa8原来是订在这里的 12-12 07:41:01.526:E/StrictMode685:at android.app.LoadedApk$ServiceDispatcher.LoadedApk.java:969 12-12 07:41:01.526:E/StrictMode685:at android.app.LoadedApk.getServiceDispatcherLoadedApk.java:863 12-12 07:41:01.526:E/StrictMode685:at android.app.ContextImpl.bindServiceContextImpl.java:1418 12-12 07:41:01.526:E/StrictMode685:at android.app.ContextImpl.bindServiceContextImpl.java:1407 12-12 07:41:01.526:E/StrictMode685:at android.content.ContextWrapper.bindServiceContextWrapper.java:473 12-12 07:41:01.526:E/StrictMode685:com.android.emailcommon.service.ServiceProxy.setTaskServiceProxy.java:157 12-12 07:41:01.526:E/StrictMode685:com.android.emailcommon.service.ServiceProxy.setTaskServiceProxy.java:145 12-12 07:41:01.526:E/StrictMode685:com.android.emailcommon.service.ServiceProxy.testServiceProxy.java:191 12-12 07:41:01.526:E/StrictMode685:com.android.exchange.ExchangeService$7.runExchangeService.java:1850 12-12 07:41:01.526:E/StrictMode685:com.android.emailcommon.utility.utility$2.doInBackgroundUtility.java:551 12-12 07:41:01.526:E/StrictMode685:com.android.emailcommon.utility.utility$2.doInBackgroundUtility.java:549 12-12 07:41:01.526:E/StrictMode685:at android.os.AsyncTask$2.callAsyncTask.java:287 12-12 07:41:01.526:E/StrictMode685:at java.util.concurrent.FutureTask.runFutureTask.java:234 12-12 07:41:01.526:E/StrictMode685:at java.util.concurrent.ThreadPoolExecutor.runWorkerThreadPoolExecutor.java:1080 12-12 07:41:01.526:E/StrictMode685:at java.util.concurrent.ThreadPoolExecutor$Worker.runThreadPoolExecutor.java:573 12-12 07:41:01.526:E/StrictMode685:at java.lang.Thread.runThread.java:856

Mysql数据库:


我的android代码在我的第二篇文章中。请检查代码并向我建议修复错误的方法。

尝试在oncreate方法中添加以下两行代码

CREATE DATABASE androidhive;

CREATE TABLE products(
pid int(11) primary key auto_increment,
name varchar(100) not null,
price decimal(10,2) not null,
description text,
created_at timestamp default now(),
updated_at timestamp
);

**php script:**

**db_config.php**

db_config.php
<?php

/*
 * All database connection variables
 */

define('DB_USER', "root"); // db user
define('DB_PASSWORD', ""); // db password (mention your db password here)
define('DB_DATABASE', "androidhive"); // database name
define('DB_SERVER', "localhost"); // db server
?>

**db_connect.php**

db_connect.php
<?php

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

}

?>

**Mysql CRUD operation:**

**Creating a row:**

**create_product.php**

<?php

/*
 * Following code will create a new product row
 * All product details are read from HTTP Post Request
 */

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

// check for required fields
if (isset($_POST['name']) && isset($_POST['price']) && isset($_POST['description'])) {

    $name = $_POST['name'];
    $price = $_POST['price'];
    $description = $_POST['description'];

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

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

    // mysql inserting a new row
    $result = mysql_query("INSERT INTO products(name, price, description) VALUES('$name', '$price', '$description')");

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

        // echoing JSON response
        echo json_encode($response);
    } else {
        // failed to insert row
        $response["success"] = 0;
        $response["message"] = "Oops! An error occurred.";

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

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

**Reading a row:**

**get_product_details.php**

<?php

/*
 * Following code will get single product details
 * A product is identified by product id (pid)
 */

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

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

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

// check for post data
if (isset($_GET["pid"])) {
    $pid = $_GET['pid'];

    // get a product from products table
    $result = mysql_query("SELECT *FROM products WHERE pid = $pid");

    if (!empty($result)) {
        // check for empty result
        if (mysql_num_rows($result) > 0) {

            $result = mysql_fetch_array($result);

            $product = array();
            $product["pid"] = $result["pid"];
            $product["name"] = $result["name"];
            $product["price"] = $result["price"];
            $product["description"] = $result["description"];
            $product["created_at"] = $result["created_at"];
            $product["updated_at"] = $result["updated_at"];
            // success
            $response["success"] = 1;

            // user node
            $response["product"] = array();

            array_push($response["product"], $product);

            // echoing JSON response
            echo json_encode($response);
        } else {
            // no product found
            $response["success"] = 0;
            $response["message"] = "No product found";

            // echo no users JSON
            echo json_encode($response);
        }
    } else {
        // no product found
        $response["success"] = 0;
        $response["message"] = "No product found";

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

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

**Reading all rows:**

**get_all_products.php**

<?php

/*
 * Following code will list all the products
 */

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

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

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

// get all products from products table
$result = mysql_query("SELECT *FROM products") or die(mysql_error());

// check for empty result
if (mysql_num_rows($result) > 0) {
    // looping through all results
    // products node
    $response["products"] = array();

    while ($row = mysql_fetch_array($result)) {
        // temp user array
        $product = array();
        $product["pid"] = $row["pid"];
        $product["name"] = $row["name"];
        $product["price"] = $row["price"];
        $product["created_at"] = $row["created_at"];
        $product["updated_at"] = $row["updated_at"];

        // push single product into final response array
        array_push($response["products"], $product);
    }
    // success
    $response["success"] = 1;

    // echoing JSON response
    echo json_encode($response);
} else {
    // no products found
    $response["success"] = 0;
    $response["message"] = "No products found";

    // echo no users JSON
    echo json_encode($response);
}
?>

**Updating a row:**

**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['pid']) && isset($_POST['name']) && isset($_POST['price']) && isset($_POST['description'])) {

    $pid = $_POST['pid'];
    $name = $_POST['name'];
    $price = $_POST['price'];
    $description = $_POST['description'];

    // 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 products SET name = '$name', price = '$price', description = '$description' WHERE pid = $pid");

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

**Deleting a row:**

**delete_product.php**

<?php

/*
 * Following code will delete a product from table
 * A product is identified by product id (pid)
 */

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

// check for required fields
if (isset($_POST['pid'])) {
    $pid = $_POST['pid'];

    // 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("DELETE FROM products WHERE pid = $pid");

    // check if row deleted or not
    if (mysql_affected_rows() > 0) {
        // successfully updated
        $response["success"] = 1;
        $response["message"] = "Product successfully deleted";

        // echoing JSON response
        echo json_encode($response);
    } else {
        // no product found
        $response["success"] = 0;
        $response["message"] = "No product found";

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

    // echoing JSON response
    echo json_encode($response);
}
?>
顺便说一句,除此之外,您可能还有其他原因无法获得输出。但我无法回答,因为您没有提供代码。如果您编辑您的问题并提供更多详细信息,如果可以,我可能会回答这些问题。

请发布您的任务
尽可能清晰地显示代码。试着调试你的代码,这样你就能知道你在哪一部分出错了。如果你粘贴那个片段会更好。上面的URL是什么?你想让其他人学习该教程,然后他们可以回答吗?我是android开发的初学者。我已经告诉过我,我下载了全部代码,并按照那里给出的建议进行了操作。此外,我使用WAMP服务器创建了MySQL数据库和php脚本。我使用localhost在本地托管它。正如Appu正确询问的代码一样,整个代码都在该链接中。这就是为什么我建议使用该URL,否则我不会。请不要误会我。检查此链接,您可以通过视频教程找到整个项目。请给我一些其他建议>以上建议不起作用。
12-12 07:41:01.496: E/ActivityThread(685): Service com.android.exchange.ExchangeService has leaked ServiceConnection com.android.emailcommon.service.ServiceProxy$ProxyConnection@40d5daa8 that was originally bound here
CREATE DATABASE androidhive;

CREATE TABLE products(
pid int(11) primary key auto_increment,
name varchar(100) not null,
price decimal(10,2) not null,
description text,
created_at timestamp default now(),
updated_at timestamp
);

**php script:**

**db_config.php**

db_config.php
<?php

/*
 * All database connection variables
 */

define('DB_USER', "root"); // db user
define('DB_PASSWORD', ""); // db password (mention your db password here)
define('DB_DATABASE', "androidhive"); // database name
define('DB_SERVER', "localhost"); // db server
?>

**db_connect.php**

db_connect.php
<?php

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

}

?>

**Mysql CRUD operation:**

**Creating a row:**

**create_product.php**

<?php

/*
 * Following code will create a new product row
 * All product details are read from HTTP Post Request
 */

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

// check for required fields
if (isset($_POST['name']) && isset($_POST['price']) && isset($_POST['description'])) {

    $name = $_POST['name'];
    $price = $_POST['price'];
    $description = $_POST['description'];

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

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

    // mysql inserting a new row
    $result = mysql_query("INSERT INTO products(name, price, description) VALUES('$name', '$price', '$description')");

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

        // echoing JSON response
        echo json_encode($response);
    } else {
        // failed to insert row
        $response["success"] = 0;
        $response["message"] = "Oops! An error occurred.";

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

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

**Reading a row:**

**get_product_details.php**

<?php

/*
 * Following code will get single product details
 * A product is identified by product id (pid)
 */

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

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

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

// check for post data
if (isset($_GET["pid"])) {
    $pid = $_GET['pid'];

    // get a product from products table
    $result = mysql_query("SELECT *FROM products WHERE pid = $pid");

    if (!empty($result)) {
        // check for empty result
        if (mysql_num_rows($result) > 0) {

            $result = mysql_fetch_array($result);

            $product = array();
            $product["pid"] = $result["pid"];
            $product["name"] = $result["name"];
            $product["price"] = $result["price"];
            $product["description"] = $result["description"];
            $product["created_at"] = $result["created_at"];
            $product["updated_at"] = $result["updated_at"];
            // success
            $response["success"] = 1;

            // user node
            $response["product"] = array();

            array_push($response["product"], $product);

            // echoing JSON response
            echo json_encode($response);
        } else {
            // no product found
            $response["success"] = 0;
            $response["message"] = "No product found";

            // echo no users JSON
            echo json_encode($response);
        }
    } else {
        // no product found
        $response["success"] = 0;
        $response["message"] = "No product found";

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

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

**Reading all rows:**

**get_all_products.php**

<?php

/*
 * Following code will list all the products
 */

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

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

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

// get all products from products table
$result = mysql_query("SELECT *FROM products") or die(mysql_error());

// check for empty result
if (mysql_num_rows($result) > 0) {
    // looping through all results
    // products node
    $response["products"] = array();

    while ($row = mysql_fetch_array($result)) {
        // temp user array
        $product = array();
        $product["pid"] = $row["pid"];
        $product["name"] = $row["name"];
        $product["price"] = $row["price"];
        $product["created_at"] = $row["created_at"];
        $product["updated_at"] = $row["updated_at"];

        // push single product into final response array
        array_push($response["products"], $product);
    }
    // success
    $response["success"] = 1;

    // echoing JSON response
    echo json_encode($response);
} else {
    // no products found
    $response["success"] = 0;
    $response["message"] = "No products found";

    // echo no users JSON
    echo json_encode($response);
}
?>

**Updating a row:**

**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['pid']) && isset($_POST['name']) && isset($_POST['price']) && isset($_POST['description'])) {

    $pid = $_POST['pid'];
    $name = $_POST['name'];
    $price = $_POST['price'];
    $description = $_POST['description'];

    // 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 products SET name = '$name', price = '$price', description = '$description' WHERE pid = $pid");

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

**Deleting a row:**

**delete_product.php**

<?php

/*
 * Following code will delete a product from table
 * A product is identified by product id (pid)
 */

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

// check for required fields
if (isset($_POST['pid'])) {
    $pid = $_POST['pid'];

    // 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("DELETE FROM products WHERE pid = $pid");

    // check if row deleted or not
    if (mysql_affected_rows() > 0) {
        // successfully updated
        $response["success"] = 1;
        $response["message"] = "Product successfully deleted";

        // echoing JSON response
        echo json_encode($response);
    } else {
        // no product found
        $response["success"] = 0;
        $response["message"] = "No product found";

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

    // echoing JSON response
    echo json_encode($response);
}
?>
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
        StrictMode.setThreadPolicy(policy);