奇if语句在android系统与阅读JSON [英] Odd if Statement in android with reading json
问题描述
所以,我读了JSON API,并获得满意的结果。基于我希望Android执行两个不同的事情的结果。
So I read a json api and get the results. Based on the results I want android to execute two different things.
如果状态=无话,我想要一个事件,如果状态=是的,我想另一个事件。
If status = no then I want one event and if status = yes I want another event.
我的code是在这里:
My code is here:
JSONObject json = new JSONObject(result);
String status = json.getString("status");
Log.d("BeforeIf", status);
if(status == "no"){
//toast logIN failed
Log.d("logIN-no", status);
String message = "Log In Failed";
Toast.makeText(c, message, Toast.LENGTH_SHORT).show();
}
else{
//get userName
Log.d("logIN-yes", "correct");
//get user ID
//set preferences
//launch normal activity
}
奇怪之处是,如果我的日志显示状态之前=无,但基于日志,似乎它转到别的...
The odd part is before the if my log shows status = no but based on the log it seems it go to the else...
06-21 22:24:05.822: D/BeforeIf(26889): no
06-21 22:24:05.877: D/logIN-yes(26889): correct
在此基础上来看,它不应该在if语句的else部分......我缺少什么笑
Based on this run it should not be in the else part of the if statement...what am I missing lol
推荐答案
您不能使用 ==
来比较两个字符串(或任何两个对象的内容此事)。你比较基准,而不是内容。
You can't use ==
to compare the contents of two Strings (or any 2 objects for that matter). You're comparing the reference, not the contents.
使用如果(status.equals(无)){
这篇关于奇if语句在android系统与阅读JSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!