无法从 Android 应用程序连接到 Apache,尽管它可以在浏览器中运行
Cannot connect to Apache from Android App, though it works from browser
过去两天我一直在尝试将我的 Android 应用程序连接到我的 Apache 服务器,但没有成功。
注意几点:
- 我可以通过 web 浏览器从每台机器连接到我的服务器
网络(防火墙未阻止连接)
- 我的服务器是在虚拟机中搭建的
我正在使用端口 8079(因为某些原因我无法连接
通过端口 80)
我在这三个“/etc/apache2/ports.conf”和"conf file of the website"和“/var/www/”中添加了"Allow from All"
- 在我的 Android 应用程序中,在此代码之前,我使用 connectivityManager class 检查我的连接并且它工作正常(它不包含在下面的代码中)
这是 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 指出需要检查堆栈跟踪。
过去两天我一直在尝试将我的 Android 应用程序连接到我的 Apache 服务器,但没有成功。
注意几点:
- 我可以通过 web 浏览器从每台机器连接到我的服务器 网络(防火墙未阻止连接)
- 我的服务器是在虚拟机中搭建的
我正在使用端口 8079(因为某些原因我无法连接 通过端口 80)
我在这三个“/etc/apache2/ports.conf”和"conf file of the website"和“/var/www/”中添加了"Allow from All"
- 在我的 Android 应用程序中,在此代码之前,我使用 connectivityManager class 检查我的连接并且它工作正常(它不包含在下面的代码中)
这是 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 指出需要检查堆栈跟踪。