Tuesday, 20 August 2013

HTTP Post not working with PHP in android

HTTP Post not working with PHP in android

I learning android and trying to write some code to verify a username and
password using a PHP script and a WAMP server. I keep getting undefined
index errors from my PHP script. As far as I can ascertain that means my
PHP script can't access the data from the URL. Here is the relevant code.
Any help is appreciated.
//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());
}
Here is my PHP script
<?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();
?>
And here a picture of the server's reply
http://imgur.com/sQStI2D
EDIT: So I figured out that even though the PHP script is giving these
errors the $username and $password variables contain the values my android
app was attempting to pass along. However the presence of these errors is
still messing with my code because the HTML for the error tables gets sent
back to the android app in the response

No comments:

Post a Comment