2017-04-21 21 views
-1

在我的android应用程序中,我需要检查从java servlet返回的字符串,并相应地改变textview。如何比较在android中从servlet接收的字符串?

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的值是“打开”,但控件进入else部分。当实际长度为4时,stat.length()返回6.这是我如何从servlet接收我的响应。

final HttpEntity entity = response.getEntity(); 
stat = EntityUtils.toString(entity); 

有人可以解释我哪里会出错吗?

+0

也许有一些空格参与 – 2017-04-21 21:04:31

+0

“_stat.length()返回6时实际长度为4_的” Java是不是骗你这件事。它可能看起来像只有4个字符,但如果长度是6,那么有6个字符。他们可能是空白的,他们可能在弦之前,他们可能在之后,但他们在那里。 – csmckelvey

回答

0

尝试使用“修剪”返回值。
例子:

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"); 
    }