내 응용 프로그램은 HTTP 요청을 실행하여 PHP 스크립트에 데이터를 제출 한 다음 응답을 기다립니다. 휴대 전화에 인터넷 연결이 있지만 연결 상태가 좋지 않으면 (잘못된 신호 또는 라우터가 인터넷에 연결되어 있지 않은 경우) 응용 프로그램이 강제로 닫힙니다. 이걸 어떻게 막을 수 있니?인터넷 응답을 기다릴 때 강제 종료
지금까지이는
사람 프레스 버튼이
이 전화가 연결되어 있는지 확인합니다 비동기 작업을 시작
(나쁜 연결이 통과) 제출 걸리는 단계입니다
HTTP 게시 실행
응답을 기다립니다.
이러한 모든 단계는 예외를 처리하는 try 및 catch로 둘러싸여 있지만 강제 종료됩니다. 여기
그냥 경우 응답 경우는 null을 확인 AsyncTask를 코드private class checkDatabase extends AsyncTask<String, Void, Boolean> {
ProgressDialog progressDialog;
protected void onPreExecute() {
super.onPreExecute();
// Starting the progress dialog
progressDialog = ProgressDialog.show(Login.this, "",
"Connecting. Please wait...", true);
}
protected Boolean doInBackground(String... info) {
// TODO Auto-generated method stub
ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo netInfo = cm.getActiveNetworkInfo();
if (netInfo != null && netInfo.isConnected()) {
String us = info[0];
String ps = info[1];
String responseText;
if (us.equals("") || ps.equals("")) {
//No username or password
return false;
} else {
try {
// Create a new HttpClient and Post Header
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(
"http://www.myfirstagent.com/android/loginquery.php");
// Add your data
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(
2);
nameValuePairs.add(new BasicNameValuePair("username",
us));
nameValuePairs.add(new BasicNameValuePair("password",
ps));
httppost.setEntity(new UrlEncodedFormEntity(
nameValuePairs));
HttpResponse response;
// Execute HTTP Post Request
try {
response = httpclient.execute(httppost);
responseText = EntityUtils.toString(response
.getEntity());
} catch (Exception e) {
//problem connecting
return false;
}
if (responseText.equals("accept")) {
return true;
} else if (responseText.equals("decline")) {
//invalid username or password
return false;
} else {
//something went wrong
return false;
}
} catch (Exception e) {
//something went wrong
return false;
}
}
} else {
//No connection
return false;
}
}
protected void onPostExecute(Boolean result) {
// Dismissing the progress dialog
if (progressDialog.isShowing())
progressDialog.dismiss();
super.onPostExecute(result);
}
}
강제 종료를 유발하는 예외는 무엇입니까? –
앱에 인터넷에 액세스 할 수있는 권한이 있습니까? –
연결이없는 곳에서 발생하기 때문에 예외가 무엇인지 잘 모르겠습니다. 잘못된 연결을 시뮬레이트하는 방법을 잘 모르겠습니다. 내 집에서만 연결이 잘되어 있기 때문입니다. – TomRichardson