Android 应用程序中的 HttpClient 请求失败,没有任何异常

HttpClient request in Android application fails without any Exceptions

我有一个 Android 应用程序,它调用部署在我笔记本电脑上的 Apache 服务器上的 java 服务器应用程序。 在家测试应用程序时,对服务器的所有调用都成功。

但是,如果我连接到不同的 WiFi(例如在咖啡馆中),来自应用程序(来自 Android 客户端应用程序)的调用将失败且没有任何错误。

直接从浏览器向服务器发出请求工作正常。

Android 发出请求的代码:

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();

        }
    }
}

执行的最后一行是:

HttpResponse httpResponse = httpClient.execute(httpGet);

在那之后,什么也没有发生。没有引发异常,也没有触发 onPostExecute 方法。

这是 URL: http://172.20.10.2:8080/Learn2LearnServer/LoginServlet?operation=login&emailAddress=myEmailAddress&password=myPassword

在我的 Android 应用程序中设置了 Internet 权限,当然,phone 在尝试时具有有效的网络连接。

对发生的事情有什么想法吗?

谢谢。

可能是您的笔记本电脑检测到它现在在 public 网络上,因此它内置的防火墙阻止了入站连接(这就是它可以在 home/private 网络上工作的原因) .如果超时设置是无限的,则症状不会发生。尝试禁用防火墙或设置连接超时。