问题
I have string to compare but I can't make it to work even I searched google and stackoverflow.
So here is my code:
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
loading.dismiss();
if(s.equalsIgnoreCase("success")){
Intent intent = new Intent(Najava.this,UserProfile.class);
intent.putExtra(USER_NAME,username);
startActivity(intent);
}else{
Toast.makeText(Najava.this,s,Toast.LENGTH_LONG).show();
}
}
But the activity never changes and the toast message shows up with the same string: http://i.stack.imgur.com/ApJLp.png
回答1:
It seems that String s has un-necessary characters present. Try below:
if(s.contains("success"){}
Note: For string comparison, its always good practice to compare constant value with other string. This will save code from Null pointers.
if("success".equalsIgnoreCase(s)){}
回答2:
Put this line before your if:
s = s.toLowerCase().trim();
or remove s from super.onPostExecute();
来源:https://stackoverflow.com/questions/32026016/android-studio-java-string-compare