2011-12-19 7 views
0

내 응용 프로그램은 HTTP 요청을 실행하여 PHP 스크립트에 데이터를 제출 한 다음 응답을 기다립니다. 휴대 전화에 인터넷 연결이 있지만 연결 상태가 좋지 않으면 (잘못된 신호 또는 라우터가 인터넷에 연결되어 있지 않은 경우) 응용 프로그램이 강제로 닫힙니다. 이걸 어떻게 막을 수 있니?인터넷 응답을 기다릴 때 강제 종료

지금까지이는

  1. 사람 프레스 버튼이

  2. 이 전화가 연결되어 있는지 확인합니다 비동기 작업을 시작

  3. (나쁜 연결이 통과) 제출 걸리는 단계입니다
  4. HTTP 게시 실행

  5. 응답을 기다립니다.

이러한 모든 단계는 예외를 처리하는 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); 
    } 
} 
+0

강제 종료를 유발하는 예외는 무엇입니까? –

+0

앱에 인터넷에 액세스 할 수있는 권한이 있습니까? –

+0

연결이없는 곳에서 발생하기 때문에 예외가 무엇인지 잘 모르겠습니다. 잘못된 연결을 시뮬레이트하는 방법을 잘 모르겠습니다. 내 집에서만 연결이 잘되어 있기 때문입니다. – TomRichardson

답변

0

입니다.

일어날 수있는 것보다 모든 것을 잡으려면 Throwable을 잡을 수 있습니다.

스택 추적을 읽는 방법을 배우십시오. 이는 최고의 조언입니다. 프로그램이 실패한 곳과 실패한 이유를 이해하는 기회를 이해해야합니다.

+0

나는이 오류를 집에서 만날 수 없으므로 오류 메시지를받을 수 없다. – TomRichardson

+1

비행 모드를 켭니다. – Snicolas

+0

나는 이것을했지만, 같은 오류가 발생하지는 않습니다. 연결 확인을 취소 할 때도 마찬가지입니다. – TomRichardson