0

I have an Android application which makes calls to a java server application deployed on a Apache server located on my laptop. When testing the application at home, all the calls to the server are successful.

If however, I connect to a different WiFi (in a cafe' for instance), the calls from the application (from the Android client app) are failing without any errors.

Making the requests to the server directly from the Browser works fine.

Android code for making the requests:

class LoginRequestTask extends AsyncTask<String, String, String> {

    /*
     * Metoda doInBackground() ruleaza pe thread-ul de fundal, in aceasta
     * medoda executandu-se request-ul la server pentru logare.
     */
    @Override
    protected String doInBackground(String... uri) {
        String output = null;
        try {

            String loginUrl = Globals.LoginServletUrl
                    + "?operation=login&emailAddress="
                    + mTxtEmail.getText() + "&password="
                    + mTxtPassword.getText();

            HttpClient httpClient = new DefaultHttpClient();
            HttpGet httpGet = new HttpGet(loginUrl);

            HttpResponse httpResponse = httpClient.execute(httpGet);
            HttpEntity httpEntity = httpResponse.getEntity();
            output = EntityUtils.toString(httpEntity);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }

        return output;
    }

    /*
     * Metoda onPostExecute() este apelata pe thread-ul UI după ce se
     * termină thread-ul de fundal. Este nevoie ca parametrul rezultat de la
     * metoda doInBackground(), verificandu-se daca s-a efectuat cu succes
     * logarea.
     */
    @Override
    protected void onPostExecute(String result) {
        super.onPostExecute(result);
        // Get response
        if (result != null && result.equals("true")) {
            PrefUtils
                    .saveToPrefs(LoginActivity.this,
                            PrefUtils.SESSION_EMAIL, mTxtEmail.getText()
                                    .toString());

            Intent i = new Intent(getApplicationContext(), HomeScreen.class);
            startActivity(i);

            Toast.makeText(getApplicationContext(), "Logare cu succes.",
                    Toast.LENGTH_SHORT).show();
        } else {
            Toast.makeText(getApplicationContext(),
                    "Logarea nu a fost realizata.", Toast.LENGTH_SHORT)
                    .show();

        }
    }
}

The last line executed is:

HttpResponse httpResponse = httpClient.execute(httpGet);

After that, nothing happens. No exceptions are raised and the onPostExecute method is not fired.

This is the URL: http://172.20.10.2:8080/Learn2LearnServer/LoginServlet?operation=login&emailAddress=myEmailAddress&password=myPassword

The Internet permission is set in my Android application and of course, the phone has a working network connection when trying.

Any ideas as to what is going on?

Thanks.

malutan.mircea
  • 143
  • 1
  • 2
  • 12
  • Maybe it's because you use DefaultHttpClient. I've seen such before. But anyhow its deprecated and you could better take another component. – greenapps Apr 28 '15 at 18:00
  • In every catch block add code like: `output = "UnsupportedEncodingException: " + e.getMessage();`. – greenapps Apr 28 '15 at 18:03
  • If you want to set a time out read: http://stackoverflow.com/questions/3000214/java-http-client-request-with-defined-timeout – greenapps Apr 28 '15 at 18:09
  • @greenapps: I'm not sure of the value adding the output = "UnsupportedEncodingException: " + e.getMessage(); would bring since when debugging no catch branch is reached. Thanks – malutan.mircea Apr 28 '15 at 18:10
  • It will hit you one day if you do not display exceptions to the user. – greenapps Apr 28 '15 at 18:12
  • it brings value in that context. unfortunately not in the context in hand. – malutan.mircea Apr 28 '15 at 18:15
  • Yes. It was not that to the point. Sorry. But also a browser times out on your link with a wrong pass and so, So i tried your link with DefaultHttpCkient and could wait for ages. By setting the timeout it returns with an IOException timed out. (Show it to the user i would say ;-). ) – greenapps Apr 28 '15 at 18:31

1 Answers1

1

It could be that your laptop is detecting it is on a public network now, and so it's built in firewall is blocking inbound connections (which is why it would work at home/private network). The symptom would be nothing happening if the timeout set is infinite. Try disabling firewall or setting a timeout on the connection.

Finn K
  • 620
  • 3
  • 8
  • Firewall was the issue. I had tested this with two different laptops. It was working on one and failing on the other. The latter had firewall on. Thanks! – malutan.mircea May 15 '15 at 16:14