Android : Login system: $_POST['username'] and $_POST['password'] are always empty

on Wednesday, March 25, 2015


Let me first start by saying that I have searched endlessly on Google for help and have literally spent the past x hours debugging the same error but I just can't figure it out.


I am following this tutorial on how to create a login system for my Android app. When I run my app on Genymotion, I am able to enter my login credentials but as soon as I hit the login button my app crashes. I ran my app again in debug mode and the cause of this was because of the following exception:



Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String org.json.JSONObject.toString()' on a null object reference



Correct me if im wrong, but I think what is happening is that my JSONParser is trying to parse an empty object. This could be because my PHP file is always returning empty and I don't know why that is.



if(isset($_POST['username'])) {
$password=$_POST["username"];
}
if(isset($_POST['password'])) {
$password=$_POST["password"];
}

if (!empty($_POST))
{
if (empty($_POST['username']) || empty($_POST['password']))
{
// Create some data that will be the JSON response
$response["success"] = 0;
$response["message"] = "One or both of the fields are empty .";

//die is used to kill the page, will not let the code below to be executed. It will also
//display the parameter, that is the json data which our android application will parse to be
//shown to the users

die(json_encode($response));
}

$query = " SELECT * FROM login WHERE username = '$username'and password='$password'";

$sql1=mysql_query($query);
$row = mysql_fetch_array($sql1);

if (!empty($row))
{
$response["success"] = 1;
$response["message"] = "You have been sucessfully login";
die(json_encode($response));
}

else
{
$response["success"] = 0;
$response["message"] = "invalid username or password ";
die(json_encode($response));

}
}

else
{
$response["success"] = 0;
$response["message"] = " One or both of the fields are empty ";
die(json_encode($response));
}

0 comments:

Post a Comment