![](/img/trans.png)
[英]Quartz+Httpclient,the job thread always be suspended without any error reports and exceptions
[英]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
Internet權限是在我的Android應用程序中設置的,當然,嘗試時手機具有有效的網絡連接。
有什么想法嗎?
謝謝。
可能是您的筆記本電腦正在檢測到它現在在公共網絡上,因此它內置的防火牆阻止了入站連接(這就是為什么它可以在家庭/專用網絡上工作的原因)。 如果超時設置為無限,則症狀不會發生。 嘗試禁用防火牆或設置連接超時。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.