比较Android中HttpResponse的结果的问题

时间:2011-10-04 02:41:19

标签: android android-asynctask httpresponse

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

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

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

提前感谢。

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

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都是有效的。

2 个答案:

答案 0 :(得分:2)

您不应该使用相等运算符来比较像

这样的字符串

尝试

result.equals("ok");

答案 1 :(得分:0)

哦,有趣!我猜你从响应实体得到的字符串包括这些东西:

<!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()); 

玩得开心:)