简体   繁体   English

登录系统:$ _POST ['username']和$ _POST ['password']始终为空

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

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. 首先,我要说的是,我在Google上进行了无休止的搜索,并花了过去的x个小时来调试相同的错误,但我无法弄清楚。

I am following this tutorial on how to create a login system for my Android app. 我正在按照本教程讲解如何为我的Android应用程序创建登录系统。 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. 当我在Genymotion上运行我的应用程序时,我能够输入我的登录凭据,但是一旦我按下登录按钮,我的应用程序就会崩溃。 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 原因:java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法'java.lang.String org.json.JSONObject.toString()'

Correct me if im wrong, but I think what is happening is that my JSONParser is trying to parse an empty object. 如果我错了,请指正我,但是我认为这是我的JSONParser试图解析一个空对象的情况。 This could be because my PHP file is always returning empty and I don't know why that is. 这可能是因为我的PHP文件总是返回空,而我不知道为什么。

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

I think you forgot to set the JSON header. 我认为您忘记设置JSON标头。 If you are returning JSON , You need to change the code like this 如果要返回JSON,则需要像这样更改代码

$data = $response;
header('Content-Type: application/json');
echo json_encode($data);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM