无法从 Android 应用程序连接到 Apache,尽管它可以在浏览器中运行

Cannot connect to Apache from Android App, though it works from browser

过去两天我一直在尝试将我的 Android 应用程序连接到我的 Apache 服务器,但没有成功。

注意几点:

这是 Android 代码。我不断收到 "caught exception" 错误。

public void tryConnection(View V) {
    HttpClient httpClient = new DefaultHttpClient();
    HttpGet httpGet = new HttpGet("http://192.168.*.*:8079/?var=hi");
    try {
        HttpResponse response = httpClient.execute(httpGet);

        if (response != null) {

            String line = "";
            InputStream inputStream = response.getEntity().getContent();
            line = convertStreamToString(inputStream);
            Toast.makeText(this, line, Toast.LENGTH_SHORT).show();
        } else {
            Toast.makeText(this, "Unable to get response", Toast.LENGTH_SHORT).show();
        }
    } catch (ClientProtocolException e) {
        Toast.makeText(this, "Caught ClientProtocolException", Toast.LENGTH_SHORT).show();
    } catch (IOException e) {
        Toast.makeText(this, "Caught IOException", Toast.LENGTH_SHORT).show();
    } catch (Exception e) {
        Toast.makeText(this, "Caught Exception", Toast.LENGTH_SHORT).show();

    }
}

private String convertStreamToString(InputStream is) {
    String line = "";
    StringBuilder total = new StringBuilder();
    BufferedReader rd = new BufferedReader(new InputStreamReader(is));
    try {
        while ((line = rd.readLine()) != null) {
            total.append(line);
        }
    } catch (Exception e) {
        Toast.makeText(this, "Stream Exception", Toast.LENGTH_SHORT).show();
    }
    return total.toString();
}

答案很简单!

我不得不使用 AsyncTask 来让它工作。感谢 tato.rodrigo 指出需要检查堆栈跟踪。