【问题标题】:Strange FileNotFoundException in AVD on Tomcat connectTomcat连接上AVD中的奇怪FileNotFoundException
【发布时间】: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


    【解决方案1】:

    好吧,我当然一直在尝试在标头中发送参数,这导致了 BS。菜鸟失误!

    bcody 来自this question 的回答对我的调试帮助很大!此外,查看 servlet 中的服务器协议可能会导致我更早地发现错误。

    这是最终起作用的代码:

    AsyncTask<Void,Void,String> getDBdata = new AsyncTask<Void, Void, String>() {
        @Override
        protected String doInBackground(Void... params) {
            URL url = null;
            try {
                url = new URL(Constants.SERVER_URL + getDBdataURL);
            } catch (MalformedURLException e) {
                e.printStackTrace();
            }
            String text;
            text = null;
            try {
                HttpURLConnection connection = (HttpURLConnection) url.openConnection();
                connection.setRequestProperty("Accept-Charset", "UTF-8");
                connection.setRequestProperty("User-Agent", "Mozilla/5.0 ( compatible ) ");
                connection.setRequestProperty("Accept", "*/*");
                connection.setChunkedStreamingMode(0);
                connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
                connection.setDoOutput(true);
                connection.setRequestMethod("POST");
                String request = "action=getDBdata";
                PrintWriter pw = new PrintWriter(connection.getOutputStream());
                pw.print(request);
                pw.close();
                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;
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-08-23
      • 2023-01-25
      • 2014-08-03
      • 1970-01-01
      • 1970-01-01
      • 2020-05-25
      相关资源
      最近更新 更多