i am doing a project in android. i am a begineer in android also. in my android application i am using php as my webservice provider. in my php code for the authentication i am check the user vaidity and return string 1 or 0. In android application i am get the string. but when i check it in a if condition it fails. is it due to any charset compatablity issue.
in php code
.....
if($auth) {
echo '1';
}
else {
echo '0';
}
in android
//get the code and saved in to string loginStatus;
if(loginStatus == "1") {
//not getting into this part if the loginStatus is 1;
}
else {
//getting into this part
}
The
==operator checks to see if two string references point to exactly the same string instance. So even if you have two strings that both have the value"1"the==operator can still returnfalse.To compare the strings character-by-character use
String.equals:To see the difference between
==andequalssee this simplified example:Result:
See this code running online: ideone
Note: if
loginStatuscan be null you also need to check for that too, or else reverse the order of the strings so that the constant string is first:Or: