如何在android中比较从servlet接收的字符串?
在我的android应用程序中,我需要检查Javaservlet返回的字符串,并相应地更改textview如何在android中比较从servlet接收的字符串?,android,http,servlets,server,httpresponse,Android,Http,Servlets,Server,Httpresponse,在我的android应用程序中,我需要检查Javaservlet返回的字符串,并相应地更改textview if(stat.equalsIgnoreCase("open")) { tx = (TextView) findViewById(R.id.textView1); tx.setText("getting"); } else { tx = (TextView) findViewById(R.id.textView1); tx.setText("not gett
if(stat.equalsIgnoreCase("open")) {
tx = (TextView) findViewById(R.id.textView1);
tx.setText("getting");
}
else {
tx = (TextView) findViewById(R.id.textView1);
tx.setText("not getting");
}
其中“stat”是从服务器返回的字符串。虽然变量stat的值为“open”,但控件进入else部分。当实际长度为4时,stat.length()也返回6。下面是我从servlet接收响应的方式
final HttpEntity entity = response.getEntity();
stat = EntityUtils.toString(entity);
有人能解释我哪里出错了吗?尝试使用“修剪”返回值。
示例:
if(stat.trim().equalsIgnoreCase("open"))
{
tx = (TextView) findViewById(R.id.textView1);
tx.setText("getting");
}
else {
tx = (TextView) findViewById(R.id.textView1);
tx.setText("not getting");
}
当实际长度为4时,“stat.length()返回6”可能包含一些空格。Java并没有就此对您撒谎。它可能看起来只有4个字符,但如果长度为6,则有6个字符。它们可能是空白,可能在字符串之前,也可能在字符串之后,但它们确实存在。