1

My android servlet is designed to post request and receive responses with a tomcat servlet on an Apache Tomcat server. For debugging, I have set up the Servlet with identical POST and GET methods so I can try the functionalities and accessability via browsers.

To cut the long story short: When I deploy the app, I can easily access it from the AVD device browser via 10.0.2.2:8080/my_app?request=test and I get a result that's just fine. Same is true for access from my machine with localhost:8080/my_app?request=test. But when I try it from my app, I always get a java.io.FileNotFoundException: http://10.0.2.2:8080/my_app. Why?

What did I try so far: The app has internet permissions and they also work, for to get to the Servlet communication point, I have to go through a login procedure via PHP first, and it's on the same server and works normally.

My AsyncTaskconnecting to the servlet looks like this:

        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;
cirko
  • 211
  • 2
  • 13

1 Answers1

2

Alright, of course I've been trying to send params in the header, and this led to BS. Rookie mistake!

bcody's answer from this question helped me a lot with debugging! Also, taking a look at the server protocol from the servlet might have led me to the error earlier.

This is the code that finally worked:

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;
    }
Community
  • 1
  • 1
cirko
  • 211
  • 2
  • 13