过去两天我一直试图将我的Android应用程序连接到我的Apache服务器,但没有运气。
有几点需要注意:
我使用端口8079(因为某些原因我无法连接 通过端口80)
我添加了#34;允许来自所有"这三个" /etc/apache2/ports.conf"以及网站的#34; conf文件"和" / var / www /"
这是Android代码。我一直得到#"异常"错误。
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();
}
答案 0 :(得分:0)
答案很简单!
我不得不使用AsyncTask来使其工作。感谢tato.rodrigo指出需要检查堆栈跟踪。