【问题标题】:How to compare String received from servlet in android?如何比较从android中的servlet接收的字符串?
【发布时间】:2017-09-18 22:26:25
【问题描述】:

在我的 android 应用程序中,我需要检查从 java servlet 返回的字符串并相应地更改 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);

谁能解释我哪里出错了?

【问题讨论】:

  • 可能涉及到一些空格
  • "stat.length() 在实际长度为 4 时返回 6" Java 并没有在这方面对您撒谎。它可能看起来只有 4 个字符,但如果长度为 6,则有 6 个字符。它们可能是空格,可能在字符串之前,也可能在字符串之后,但它们就在那里。

标签: android http servlets server httpresponse


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-06-18
    • 2013-09-02
    • 2021-01-11
    • 1970-01-01
    • 2020-01-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多