【发布时间】:2015-12-22 07:07:55
【问题描述】:
我的 android servlet 旨在通过 Apache Tomcat 服务器上的 tomcat servlet 发布请求和接收响应。为了调试,我使用相同的 POST 和 GET 方法设置了 Servlet,因此我可以通过浏览器尝试功能和可访问性。
长话短说:当我部署应用程序时,我可以通过10.0.2.2:8080/my_app?request=test 从 AVD 设备浏览器轻松访问它,我得到的结果很好。从我的机器使用localhost:8080/my_app?request=test. 访问也是如此
但是当我从我的应用程序中尝试它时,我总是得到一个java.io.FileNotFoundException: http://10.0.2.2:8080/my_app。
为什么?
到目前为止我做了什么:该应用程序具有互联网权限并且它们也可以工作,因为要到达 Servlet 通信点,我必须先通过 PHP 进行登录程序,并且它在同一台服务器上并且可以正常工作.
我的AsyncTask连接到 servlet 如下所示:
AsyncTask<Void,Void,String> getDBdata = new AsyncTask<Void, Void, String>() {
@Override
protected String doInBackground(Void... params) {
URL url = null;
try {
url = new URL("http://10.0.2.2:8080/my_app");
} catch (MalformedURLException e) {
e.printStackTrace();
}
String text;
text = null;
JsonArray js = null;
try {
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDoOutput(true);
connection.setRequestMethod("POST");
connection.setRequestProperty("action", "getDBData");
connection.setDoInput(true);
connection.connect();
BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
StringBuilder builder = new StringBuilder();
String aux = "";
while ((aux = in.readLine()) != null) {
builder.append(aux);
}
text = builder.toString();
} catch (IOException e) {
e.printStackTrace();
}
return text;
【问题讨论】:
标签: java android tomcat servlets