2012-12-11 34 views
-2
I am using the code I found in the following tutorial to connect an Android application with a MySQL database with the help of a php script I found here 

http://www.androidhive.info/2012/05/how-to-connect-android-with-php-mysql/ 我下載的代碼MySQL數據庫連接Android和試圖執行的emulator.My應用程序的項目在仿真器中創建,我能夠做到mainscreenactivity但不能夠移動到下一個活動來做CRUD操作。在模擬器中,加載之後說不幸的是, MainScreen has stopped作爲一個彈出消息。我的應用程序名稱是MainScreen。我檢查了我的Eclipse LogCat,它顯示以下錯誤。如果有人能幫我解決這個問題,我會很感激。錯誤在日食發生logcat中同時使用PHP 1

logcat的:

12-12 07:41:01.496: E/ActivityThread(685): Service com.android.exchange.ExchangeService has leaked ServiceConnection [email protected]5daa8 that was originally bound here 

7月12日至12日:41:01.496:E/ActivityThread(685):android.app.ServiceConnectionLeaked:服務com.android.exchange.ExchangeService已泄漏ServiceConnection融爲一體。 [email protected]8最初在這裏綁定 12-12 07:41:01.496:E/ActivityThread(685):在android.app.LoadedApk $ ServiceDispatcher。(LoadedApk.java:969) 12-12 07:41:01.496:E/ActivityThread(685):在android.app.LoadedApk.getServiceDispatcher(LoadedApk.java:863) 12-12 07:41:01.496:E/ActivityT hread(685):at android.app.ContextImpl.bindService(ContextImpl.java:1418) 12-12 07:41:01.496:E/ActivityThread(685):at android.app.ContextImpl.bindService(ContextImpl.java: 1407) 12-12 07:41:01.496:E/ActivityThread(685):at android.content.ContextWrapper.bindService(ContextWrapper.java:473) 12-12 07:41:01.496:E/ActivityThread(685) :at com.android.emailcommon.service.ServiceProxy.setTask(ServiceProxy.java:157) 12-12 07:41:01.496:E/ActivityThread(685):at com.android.emailcommon.service.ServiceProxy.setTask( ServiceProxy.java:145) 12-12 07:41:01.496:E/ActivityThread(685):at com.android.emailcommon.service.ServiceProxy.test(ServiceProxy.java:191) 12-12 07:41: 01.496:E/ActivityThread(685):at com.android.exchange.ExchangeService $ 7.run(ExchangeService.java:1850) 12-1 2 07:41:01.496:E/ActivityThread(685):at com.android.emailcommon.utility.Utility $ 2.doInBackground(Utility.java:551) 12-12 07:41:01.496:E/ActivityThread(685) :at com.android.emailcommon.utility.Utility $ 2.doInBackground(Utility.java:549) 12-12 07:41:01.496:E/ActivityThread(685):at android.os.AsyncTask $ 2.call(AsyncTask。 java:287) 12-12 07:41:01.496:E/ActivityThread(685):at java.util.concurrent.FutureTask.run(FutureTask.java:234) 12-12 07:41:01.496:E/ActivityThread(685):在java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080) 12-12 07:41:01.496:E/ActivityThread(685):位於java.util.concurrent.ThreadPoolExecutor $ Worker。運行(ThreadPoolExecutor.java:573) 12-12 07:41:01.496:E/ActivityThread(685):at java.lang.Thread.run(Thread.java:856) 12-12 07:41:01.526:E/StrictMode(685):空 七月12日至12日:41:01.526:E/StrictMode(685):android.app.ServiceConnectionLeaked:服務com.android.exchange.ExchangeService已泄漏ServiceConnection融爲一體。 [email protected]8最初綁定在這裏 12-12 07:41:01.526:E/StrictMode(685):在android.app.LoadedApk $ ServiceDispatcher。(LoadedApk.java:969) 7月12日至12日:41:01.526:E/StrictMode(685):在android.app.LoadedApk.getServiceDispatcher(LoadedApk.java:863) 7月12日至12日:41:01.526:E/StrictMode(685):在機器人.app.ContextImpl.bindService(ContextImpl.java:1418) 7月12日至12日:41:01.526:E/StrictMode(685):在android.app.ContextImpl.bindService(ContextImpl.java:1407) 7月12日至12日:41:01.526:E/StrictMode(685):在android.content.ContextWrapper.bindService(ContextWrapper.java:473 ) 12-12 07:41:01.526:E/StrictMode(685):at com.android.emailcommon.service.ServiceProxy。setTask(ServiceProxy.java:157) 12-12 07:41:01.526:E/StrictMode(685):at com.android.emailcommon.service.ServiceProxy.setTask(ServiceProxy.java:145) 12-12 07: 41:01.526:E/StrictMode(685):at com.android.emailcommon.service.ServiceProxy.test(ServiceProxy.java:191) 12-12 07:41:01.526:E/StrictMode(685):at com。 android.exchange.ExchangeService $ 7.run(ExchangeService.java:1850) 12-12 07:41:01.526:E/StrictMode(685):at com.android.emailcommon.utility.Utility $ 2.doInBackground(Utility.java: 551) 12-12 07:41:01.526:E/StrictMode(685):at com.android.emailcommon.utility.Utility $ 2.doInBackground(Utility.java:549) 12-12 07:41:01.526:E/StrictMode(685):at android.os.AsyncTask $ 2.call(AsyncTask.java:287) 12-12 07:41:01.526:E/StrictMode(685):at java.util.concurrent.FutureTask.ru n(FutureTask.java:234) 12-12 07:41:01.526:E/StrictMode(685):在java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080) 12-12 07:41: 01.526:E/StrictMode(685):在java.util.concurrent.ThreadPoolExecutor $ Worker.run(ThreadPoolExecutor.java:573) 12-12 07:41:01.526:E/StrictMode(685):at java.lang。 Thread.run(Thread.java:856)

MySQL數據庫:

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

我的Android代碼是在我的第二個post.Please檢查代碼,並建議我的東西來修復錯誤。

+0

請儘可能清楚地發佈您的問題代碼。嘗試調試你的代碼,這樣你就會知道你哪裏出錯了。如果您粘貼該代碼段,這是一個更好的做法。 – Kanth

+0

以上URL是什麼?你希望別人遵循這個教程,然後他們可以回答? – Enthusiast

+0

我是一名android開發人員的初學者。我已經告訴我,我下載了完整的代碼,並按照給出的建議進行了操作。此外,我使用WAMP服務器來創建MySQL數據庫和php腳本。我使用localhost在本地託管它。問碼的權利,整個代碼是在那個鏈接。這就是爲什麼我建議這個URL,否則我不won.ton't錯誤我Kanth –

回答

0

嘗試在oncreate()方法中添加以下兩行代碼。

StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build(); 
     StrictMode.setThreadPolicy(policy); 

順便說一句,你可能還有其他原因也沒有得到輸出,除此之外。但是我無法回答,因爲你沒有提供你的代碼。如果你用更多的細節編輯你的問題,我可能會回答那些問題。

+0

檢查此鏈接http://www.androidhive.info/2012/05/如何連接Android的與PHP中的MySQL /你可以找到整個項目與視頻教程。請給我一些其他建議>上面的一個是行不通的。 –