2014-06-23 95 views
-1

我工作的一個Android應用程序,它發送請求到服務器get some data,但問題是,當我失去了網絡連接我的應用程序得到墜毀。或者如果我忘記更改IP地址,則應用程序也會崩潰。我只在HTTPGET的演員中觀察到此問題,而不是HTTPPOST。我也檢查Network Availability但它仍然保持墜毀。當我的應用程序丟失連接而不是崩潰時,我想向用戶顯示Toast消息。我正在分享下面的snappet。有人可以請回顧一下,理清我的問題。我不知道我在哪裏出了錯應用程序崩潰調用

CODE:

protected Void doInBackground(String... params) { 
      // TODO Auto-generated method stub 
      cellphoneDate = params[0]; 

       Log.e("Alert---Service","Calling Service now (: "); 
       //Calling service now 
       DefaultHttpClient httpClient = new DefaultHttpClient(); 
       HttpGet httpGet = new HttpGet(GETTING_ALERTS_URL + "/" 
         + cellphoneDate); 
       HttpResponse httpResponse = null; 
       try { 
        httpResponse = httpClient.execute(httpGet); 

       } catch (ClientProtocolException e) { 
        // TODO Auto-generated catch block 
        e.printStackTrace(); 
       } catch (IOException e) { 
        // TODO Auto-generated catch block 
        e.printStackTrace(); 
       } 

       isInternetPresent = cm.isConnected(); 
       if(isInternetPresent) 
       { 
        httpEntity = httpResponse.getEntity();  
        try { 
         Serv_Response = EntityUtils.toString(httpEntity); 
        } catch (ParseException e) { 
         // TODO Auto-generated catch block 
         e.printStackTrace(); 
        } catch (IOException e) { 
         // TODO Auto-generated catch block 
         e.printStackTrace(); 
        } 
        try { 
         if (Serv_Response != null) { 
          ////////////////////////////new code for getting list /////////////////// 
          JSONObject jsonObj1 = new JSONObject(Serv_Response); 
          JSONArray alertName = jsonObj1.getJSONArray(TAG_NAME); 

          for (int i = 0; i < alertName.length(); i++) { 
           JSONObject c = alertName.getJSONObject(i); 
           String alert_title = c.getString(TAG_ALERT_TITLE); 
           Alerts alertObject = new Alerts(); 
           alertObject.setAlertTitle(alert_title); 
           alertsList.add(alertObject); 
           } 

         } 
         } catch (JSONException e) { 
         // TODO: handle exception 
         e.printStackTrace(); 
        } 
       } 

      return null; 
     } 
protected void onPostExecute(Void result) { 
      // TODO Auto-generated method stub 
      super.onPostExecute(result); 
     // Toast.makeText(getBaseContext(), "From Database :" + Serv_GettingQuiz_Response, Toast.LENGTH_LONG).show(); 
      //String array[] = new String[size]; 
      if(Serv_Response.equals("{\"DoitResult\":[]}")) 
      { 
       //DoNothing 
      } 

      for(int i = 0; i < alertsList.size() ; i++) 
      { 
       Log.e("Alert---Serice", "POP-UP notification"); 
      showNotification(alertsList.get(i).getAlertTitle(), "TAP for More Details", i); 
      flag = true; 
      // savingDate(Serv_GettingQuiz_Response); 
      } 
     } 

logcat的:

06-23 23:53:53.625: W/System.err(8610): org.apache.http.conn.HttpHostConnectException: Connection to http://192.168.1.9 refused 
06-23 23:53:53.635: W/System.err(8610):  at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:183) 
06-23 23:53:53.635: W/System.err(8610):  at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164) 
06-23 23:53:53.645: W/System.err(8610):  at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119) 
06-23 23:53:53.645: W/System.err(8610):  at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:367) 
06-23 23:53:53.655: W/System.err(8610):  at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:754) 
06-23 23:53:53.655: W/System.err(8610):  at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:519) 
06-23 23:53:53.655: W/System.err(8610):  at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:497) 
06-23 23:53:53.665: W/System.err(8610):  at com.smartclasss.alerts.MyService$DoInBackground.doInBackground(MyService.java:169) 
06-23 23:53:53.675: W/System.err(8610):  at com.smartclasss.alerts.MyService$DoInBackground.doInBackground(MyService.java:1) 
06-23 23:53:53.675: W/System.err(8610):  at android.os.AsyncTask$2.call(AsyncTask.java:288) 
06-23 23:53:53.675: W/System.err(8610):  at java.util.concurrent.FutureTask.run(FutureTask.java:237) 
06-23 23:53:53.685: W/System.err(8610):  at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231) 
06-23 23:53:53.685: W/System.err(8610):  at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112) 
06-23 23:53:53.695: W/System.err(8610):  at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587) 
06-23 23:53:53.695: W/System.err(8610):  at java.lang.Thread.run(Thread.java:864) 
06-23 23:53:53.695: W/System.err(8610): Caused by: java.net.ConnectException: failed to connect to /192.168.1.9 (port 80): connect failed: ENETUNREACH (Network is unreachable) 
06-23 23:53:53.715: W/System.err(8610):  at libcore.io.IoBridge.connect(IoBridge.java:114) 
06-23 23:53:53.715: W/System.err(8610):  at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:192) 
06-23 23:53:53.715: W/System.err(8610):  at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:459) 
06-23 23:53:53.725: W/System.err(8610):  at java.net.Socket.connect(Socket.java:873) 
06-23 23:53:53.725: W/System.err(8610):  at org.apache.http.conn.scheme.PlainSocketFactory.connectSocket(PlainSocketFactory.java:125) 
06-23 23:53:53.735: W/System.err(8610):  at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:144) 
06-23 23:53:53.735: W/System.err(8610):  ... 14 more 
06-23 23:53:53.735: W/System.err(8610): Caused by: libcore.io.ErrnoException: connect failed: ENETUNREACH (Network is unreachable) 
06-23 23:53:53.755: W/System.err(8610):  at libcore.io.Posix.connect(Native Method) 
06-23 23:53:53.755: W/System.err(8610):  at libcore.io.BlockGuardOs.connect(BlockGuardOs.java:85) 
06-23 23:53:53.755: W/System.err(8610):  at libcore.io.IoBridge.connectErrno(IoBridge.java:127) 
06-23 23:53:53.765: W/System.err(8610):  at libcore.io.IoBridge.connect(IoBridge.java:112) 
06-23 23:53:53.765: W/System.err(8610):  ... 19 more 
+1

每當發生崩潰時,它都會幫助發佈堆棧跟蹤。否則,我們無法給你提供很好的建議。 –

+0

@SkyKelsey感謝您的回覆,每當我的應用程序崩潰時,它都會捕獲異常,但不會執行onPostExecute()函數。爲什麼這樣? –

+1

添加你的堆棧跟蹤! – amd

回答

0

您需要檢查互聯網連接,你執行你的請求之前。另外,如果執行請求有異常,不要只打印堆棧跟蹤,還需要停止處理請求。

+0

謝謝我會試試這個,然後讓你知道(: –