2012-02-20 68 views
2

在調試WCF服務時,我遇到了這個問題。我使用一個HttpURLConnection與服務進行通信。在調試服務時,我遇到了一個問題,即當Android應用程序等待響應時,必須停止服務器,導致Android應用程序意外停止。當連接到服務器丟失時,Android應用程序崩潰

什麼是最好的方法來處理服務器意外突然停止,並讓Android客戶端優雅地處理它。我請求/通過輸入反應的的AsyncTask在後臺工作:

@Override 
    protected JSONObject doInBackground(BLHttpJSONPostModel ...models) { 
     JSONObject jsonObject = null; 

     BLHttpJSONPostModel postModel = models[0]; 
     URL url = null; 
     byte[] postData = null; 
     String protocol = null; 
     String response = null; 
     InputStream responseStream = null; 
     OutputStream outStream = null; 
     HttpURLConnection connection = null; 
     BufferedReader bufferedReader = null; 
     StringBuilder builder = null; 

     try { 
      url = new URL(postModel.getUrlString()); 
      postData = postModel.getPostDataAsJsonObject().toString().getBytes(); 
      protocol = url.getProtocol(); 
     } catch (MalformedURLException e1) { 
      e1.printStackTrace(); 
      return null; 
     } 

     if (protocol.equals("https")) { 
      connection = this.setUpHttps(url); // establishes a secure connection if protocol is HTTPS 
     } 
     else if (protocol.equals("http")) { 
      try { 
       connection = (HttpURLConnection)url.openConnection(); // establishes un-secured connection 
      } catch (IOException e) { 
       e.printStackTrace(); 
       return null; 
      } 
     } 
     else 
      return null; // handles unsupported protocol 

     try { 
      connection.setRequestProperty("Content-Length", String.valueOf(postData.length)); 
      connection.setRequestProperty("CONTENT-TYPE", "application/json"); 
      connection.setRequestMethod("POST"); 
      connection.setUseCaches(false); 
      connection.setDoInput(true); 
      connection.setDoOutput(true); 
      connection.connect(); 
      outStream = connection.getOutputStream(); 
      outStream.write(postData); 
      outStream.close(); 

      String _type = connection.getContentType();   // for debugging only 
      String _message = connection.getResponseMessage(); // "" 
      int size = connection.getContentLength();   // "" 

      bufferedReader = new BufferedReader(new InputStreamReader(connection.getInputStream(), "UTF-8")); 
      builder = new StringBuilder(); 

      while((response = bufferedReader.readLine()) != null) { 
       builder.append(response); 
      } 
     } catch (IOException e) { 
      e.printStackTrace(); // no exceptio caught here 
     } finally { 
      connection.disconnect(); 
     } 

     if ((builder != null) && (! builder.toString().equals(""))) { 
      try { 
       jsonObject = new JSONObject(builder.toString()); 
      } catch (JSONException e) { 
       e.printStackTrace(); 
      } 
     } 

     return jsonObject; 
    } 

堆棧跟蹤:

02-20 12:13:33.935: E/AndroidRuntime(1173): FATAL EXCEPTION: AsyncTask #1 
02-20 12:13:33.935: E/AndroidRuntime(1173): java.lang.RuntimeException: An error occured while executing doInBackground() 
02-20 12:13:33.935: E/AndroidRuntime(1173):  at android.os.AsyncTask$3.done(AsyncTask.java:200) 
02-20 12:13:33.935: E/AndroidRuntime(1173):  at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273) 
02-20 12:13:33.935: E/AndroidRuntime(1173):  at java.util.concurrent.FutureTask.setException(FutureTask.java:124) 
02-20 12:13:33.935: E/AndroidRuntime(1173):  at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307) 
02-20 12:13:33.935: E/AndroidRuntime(1173):  at java.util.concurrent.FutureTask.run(FutureTask.java:137) 
02-20 12:13:33.935: E/AndroidRuntime(1173):  at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1068) 
02-20 12:13:33.935: E/AndroidRuntime(1173):  at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:561) 
02-20 12:13:33.935: E/AndroidRuntime(1173):  at java.lang.Thread.run(Thread.java:1096) 
02-20 12:13:33.935: E/AndroidRuntime(1173): Caused by: java.lang.NullPointerException 
02-20 12:13:33.935: E/AndroidRuntime(1173):  at java.io.Reader.<init>(Reader.java:65) 
02-20 12:13:33.935: E/AndroidRuntime(1173):  at java.io.InputStreamReader.<init>(InputStreamReader.java:93) 
02-20 12:13:33.935: E/AndroidRuntime(1173):  at com.beslogic.remotpayment.connection.PostJSONTask.doInBackground(PostJSONTask.java:136) 
02-20 12:13:33.935: E/AndroidRuntime(1173):  at com.beslogic.remotpayment.connection.PostJSONTask.doInBackground(PostJSONTask.java:1) 
02-20 12:13:33.935: E/AndroidRuntime(1173):  at android.os.AsyncTask$2.call(AsyncTask.java:185) 
02-20 12:13:33.935: E/AndroidRuntime(1173):  at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305) 
02-20 12:13:33.935: E/AndroidRuntime(1173):  ... 4 more 
+0

你捕獲了堆棧跟蹤嗎? – 2012-02-20 17:04:53

+0

@NickCampion更新了堆棧跟蹤。 – 2012-02-20 17:21:07

回答

1

答案很簡單(通常是這樣)。

如果BufferedReader被實例化,由於連接中斷,connection.getInputStream()返回null。

它應該是:

responseStream = connection.getInputStream(); 

if (responseStream != null) { 
    bufferedReader = new BufferedReader(new InputStreamReader(responseStream, "UTF-8")); 
    builder = new StringBuilder(); 

    while((response = bufferedReader.readLine()) != null) { 
     builder.append(response); 
    } 
} 

這被扔,這不是被抓運行時異常。

1

我有太多經常面對這個問題。該問題(出於某種原因)是您正在捕獲的Exception對象e可能爲空。在你的try catch語句,這樣做:

try { 
    // Network code 
} catch(Exception e) { 
    if(e != null) { 
     e.printStackTrace(); 
    } else { 
     Log.i(appLogTag, "Error: I/O Error"); 
    } 
} 

做它同時爲try ... catch塊。我知道我現在的解決方案聽起來很荒謬,但我經常遇到這種情況,以至於認爲你可能面臨同樣的問題。

+0

調試器甚至沒有在catch塊中打破。 – 2012-02-20 17:28:22

相關問題