簡體   English   中英

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

[英]String comparison in onPostexecute not working

以下代碼是我為意圖添加的,結果是從login.php返回的內容。 但是它無法比較是否成功。 在任何情況下都不會進入if循環。 但是,它在警報對話框中顯示“登錄成功”。

@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,我最終會返回一些符號,例如服務器特定的回車符等等。

嘗試這個

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

代替

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

我認為您應該檢查“登錄成功”而不是“注冊成功...”

並且還在PHP端使用布爾值作為返回類型。 像狀態為真/假。

不要使用字符串作為狀態。

在PHP方面:

echo true;  
else
echo false;

像那樣。

在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