我在學習android並嘗試使用PHP腳本和WAMP服務器編寫一些代碼來驗證用戶名和密碼。我不斷從我的PHP腳本獲取未定義的索引錯誤。據我所知,這意味着我的PHP腳本無法訪問URL中的數據。這是相關的代碼。任何幫助表示讚賞。HTTP Post不能在Android中使用PHP
//build url data to be sent to server
ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("username",username));
nameValuePairs.add(new BasicNameValuePair("password",password));
String result = "";
InputStream is = null;
//http post
try{
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://10.0.2.2/PasswordCheck.php");
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
is = entity.getContent();
}catch(Exception e){
Log.e("Connection", "Error in http connection "+e.toString());
}
這裏是我的PHP腳本
<?php
mysql_connect("localhost", "root", "") or die("could not connect to mysql");
mysql_select_db("drop-in") or die("database not found");
$username = $_POST["username"];
$suppliedPassword = $_POST["password"];
$databasePassword = "";
$output = "false";
$query = mysql_query("SELECT Password FROM users WHERE Username = '$username'") or die("query failed");
if(mysql_num_rows($query) > 0){
$row = mysql_fetch_assoc($query);
$databasePassword = $row['password'];
if($databasePassword == $suppliedPassword)
{
$output = "true";
}
}
print($output);
mysql_close();
?>
這裏服務器的答覆
編輯的圖片:所以我想通了,即使PHP腳本所賜這些錯誤的$用戶名和$密碼變量包含我的Android應用程序試圖傳遞的值。然而,這些錯誤的存在仍然與我的代碼混亂,因爲錯誤表的HTML被髮回到Android應用程序的響應
ArrayList根據需要增長,所以這不是問題。不過謝謝。 –