简体   繁体   English

onPostexecute中的字符串比较不起作用

[英]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. 以下代码是我为意图添加的,结果是从login.php返回的内容。 But it fails to compare if it is success or not. 但是它无法比较是否成功。 It does not enter the if loop in any case. 在任何情况下都不会进入if循环。 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 的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. 并且还在PHP端使用布尔值作为返回类型。 like status true/false. 像状态为真/假。

Don't use string as status. 不要使用字符串作为状态。

At PHP side: 在PHP方面:

echo true;  
else
echo false;

Like that. 像那样。

At Android side: 在Android方面:

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

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

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