android - 在我的 android 应用程序上,即使我从数据库中获得真实值(value),我也无法登录

标签 android mongodb if-statement

用户名和密码正确,但“if(username=...)”此 block 不起作用。也没有错误。它向 onPostExecute() 方法返回 0 值。

@Override
public Integer doInBackground(Void... Params){
    try{
           ....
            if(userName=="sinan" && password=="123456")
            {
                returnValue=1;
                break;
            }
            else
            {
                returnValue=0;
            }
        }
        return returnValue;
    }
    catch (Exception ex)
    {
        return (-1);
    }
}

阿斯达斯

   @Override
    protected void onPostExecute(Integer result){ 
  super.onPostExecute(result);      
  if(result==1)
    {
        txtViewUserName.setText(userName);
        txtViewPassword.setText(password);
    }
    else if(result.intValue()==0)
    {
        txtViewUserName.setText("Result 0");
    }
    ....
}

最佳答案

你的错误是你在使用==操作符,==操作符比较对象在内存中的位置

使用 equals() 方法代替 == 运算符比较两个字符串对象

   Override
     public Integer doInBackground(Void... Params){
         try{
       ....
        if(userName.equals("sinan") && password.equals("123456"))
        {
            returnValue=1;
            break;
        }
        else
        {
            returnValue=0;
        }
    }
    return returnValue;
}
catch (Exception ex)
{
    return (-1);
}

关于android - 在我的 android 应用程序上,即使我从数据库中获得真实值(value),我也无法登录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30353412/

相关文章:

android - Android Studio启动模拟器报错

android - 如何在列表中添加新项目时更新 RecyclerView 适配器数据

node.js - 具有数组对象值总和的 MongoDB 聚合

node.js - 这是结合 MongoDB 和 Redis 的好方案吗?

php - 变量中的 If 语句

android - 可以将 sharedPrefrence 与 Coroutine kotlin 一起使用吗

mongodb - 检查字段是否存在于数组的子文档中

JavaScript - 循环内的 If 语句出现问题

c - 如何使用 IF 语句有选择地输出数组的内容

android - 在首选项中添加搜索栏