比较 Android 中 HttResponse 结果的问题

发布于 2024-12-07 18:40:26 字数 1789 浏览 2 评论 0原文

我在尝试将 HttpResponse 的结果与简单字符串进行比较时遇到问题。

下面的代码所做的只是获取 Http 请求的响应。 在这种情况下,请求的结果是一个简单的“ok”,但是当我尝试将其与另一个字符串进行比较时,条件不起作用。

我可以通过 toast 消息显示响应...来调试它并确认它是我所期望的,但我不知道为什么条件不起作用。

提前致谢。

<代码> 导入到这里...

public class HttpTest extends Activity {

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    DownloadWebPageTask task = new DownloadWebPageTask();
    task.execute(new String[] { "http://www.ecoeficiencia-ambiental.com/test/" });
}
private class DownloadWebPageTask extends AsyncTask<String, Void, String> {
    protected String doInBackground(String... urls) {
        String response = "";
        for (String url : urls) {
            DefaultHttpClient client = new DefaultHttpClient();
            HttpGet httpGet = new HttpGet(url);
            try {
                HttpResponse execute = client.execute(httpGet);
                InputStream content = execute.getEntity().getContent();
                BufferedReader buffer = new BufferedReader(new InputStreamReader(content));
                String s = "";
                while ((s = buffer.readLine()) != null) {
                    response += s;
                }

            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return response;
    }
    protected void onPostExecute(String result) {
        if(result == "ok"){
            Toast.makeText(HttpTest.this, result, Toast.LENGTH_LONG).show();
        }else{
            Toast.makeText(HttpTest.this, "the conditional fails, the result is: "+result, Toast.LENGTH_LONG).show();
        }
    }
}

}

注意:清单具有使用互联网的权限。 代码和 URL 均有效。

I'm having an issue trying to compare the result of an HttpResponse with an simple string.

What the code below do, is just get the response of an Http request.
In this case the result of the request is a simple "ok", but when I try to compare it with another string the conditional doesn't work.

I'm able to show the response via toast message...to debug it and to confirm that it is what I'm expecting, but I don't know why the conditional is not working.

Thank's in advance.


imports go here...

public class HttpTest extends Activity {

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    DownloadWebPageTask task = new DownloadWebPageTask();
    task.execute(new String[] { "http://www.ecoeficiencia-ambiental.com/test/" });
}
private class DownloadWebPageTask extends AsyncTask<String, Void, String> {
    protected String doInBackground(String... urls) {
        String response = "";
        for (String url : urls) {
            DefaultHttpClient client = new DefaultHttpClient();
            HttpGet httpGet = new HttpGet(url);
            try {
                HttpResponse execute = client.execute(httpGet);
                InputStream content = execute.getEntity().getContent();
                BufferedReader buffer = new BufferedReader(new InputStreamReader(content));
                String s = "";
                while ((s = buffer.readLine()) != null) {
                    response += s;
                }

            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return response;
    }
    protected void onPostExecute(String result) {
        if(result == "ok"){
            Toast.makeText(HttpTest.this, result, Toast.LENGTH_LONG).show();
        }else{
            Toast.makeText(HttpTest.this, "the conditional fails, the result is: "+result, Toast.LENGTH_LONG).show();
        }
    }
}

}

Note: the manifest has the permission to use internet.
both the code and the URL are functional.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

哎呦我呸! 2024-12-14 18:40:26

您不应该使用相等运算符来比较这样的字符串

Try

result.equals("ok");

You should not use the equality operator to compare strings like that

Try

result.equals("ok");
锦上情书 2024-12-14 18:40:26

哦,有趣!我猜想您从响应实体获得的字符串也包括这些内容:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>

不仅仅是您想象的单个字符串“ok”。这就是比较失败的原因。
您可以通过以下方式确认回复:

string response = EntityUtils.toString(execute.getEntity()); 

玩得开心:)

Oh, interesting! I guess that the string you get from reponse Entity including these things as well:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>

Not just a single string 'ok' as you think. That's why comparison fails.
You can confirm reponse by:

string response = EntityUtils.toString(execute.getEntity()); 

Have fun :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文