简体   繁体   中英

String comparison in onPostexecute not working

Following code is what I added for intent and result is something that is returned from my login.php. But it fails to compare if it is success or not. It does not enter the if loop in any case. It shows "Login Success" in the alert dialog though.

@Override
    protected void onPostExecute(String result) {
            if(result.equals("Login Success")){
                Intent intent = new Intent();
                intent.setClassName("com.example.soumya.attendance","com.example.soumya.attendance.LoginActivity");
                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                ctx.startActivity(intent);}
            else{
                alertDialog.setMessage("No Condition Matched..");
                alertDialog.show();
            }

    }

Login.php

<?php  
 require "init.php";  
 $uname = $_POST["login_name"];  
 $password =  $_POST["login_pass"];  
 $sql_query = "select name from students where uname like '$uname' and password like '$password';";  
$result = mysqli_query($con,$sql_query);  
 if(mysqli_num_rows($result) >0 )  
 {  
 $row = mysqli_fetch_assoc($result);  
 $name =$row["name"];  
 echo "Login Success";  
 }  
 else  
 {   
 echo "Login Failed.......Try Again..";  
 }  
 ?>  

尝试使用result.contains("Login Success")而不是result.equals()作为PHP,我最终会返回一些符号,例如服务器特定的回车符等等。

try this

result.contains("Login Success"){ // contains will check the availability of Login Success.
//Your Code
}

instead of

if(result.equals("Login Success")){  // check the Complete String .
  //Your Code
}

I think you should check "Login Success" instead of "Registration Success..."

And also use boolean value for return type at PHP side. like status true/false.

Don't use string as status.

At PHP side:

echo true;  
else
echo false;

Like that.

At Android side:

if(result)
{
    Toast.makeText(ctx, "Login Success", Toast.LENGTH_LONG).show();
} else {
...
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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