问题描述
我很难在使用Java的Android中比较两个字符串,我正在执行HTTP get请求,该请求返回yes或no,并据此决定是否启动新活动.
I am having hard time comparing two strings in Android using Java, what I am doing is running HTTP get request which return yes or no and based on that I decide whether to launch a new activity or not.
我正在Async onPostExecute方法中执行字符串比较,尽管HTTP返回的字符串是,但它对我不起作用.
I am performing the string comparison inside Async onPostExecute method and though the HTTP returned string is yes it is not working for me.
代码在这里
protected void onPostExecute(String result) {
progressDialog.dismiss();
if(result.equals("yes"))
{
Intent toDashboard = new Intent(LoginActivity.this,Dashboard.class);
startActivity(toDashboard);
Toast.makeText(LoginActivity.this, "It was yes", Toast.LENGTH_LONG).show();
}
else
{
Toast.makeText(LoginActivity.this, result, Toast.LENGTH_LONG).show();
}
}
尽管返回的字符串为yes,它仍会跳过if语句并创建一个显示是"的祝酒词.
Though the returned string is yes it still skips the if statement and creates a toast showing "Yes".
我还尝试了其他匹配字符串的方法,它们都在这里
I have also tried some other ways to match a string and they all goes here
if(result == "yes")
if(result.toString().equals("yes"))
if(result.contentEquals("yes"))
if(result.equalsIgnoreCase("yes"))
他们都不适合我
推荐答案
结果中可能会有空白.
尝试修剪它.
result.trim().equals("yes")
这篇关于Android字符串比较无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!