Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/http/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在android中比较从servlet接收的字符串?_Android_Http_Servlets_Server_Httpresponse - Fatal编程技术网

如何在android中比较从servlet接收的字符串?

如何在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

在我的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 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个字符。它们可能是空白,可能在字符串之前,也可能在字符串之后,但它们确实存在。