2012-01-02 65 views
0

我在Async-task中使用以下代碼來解析web服務並在地圖中顯示結果(在HTC願望s中爲真實設備)。在從webservice獲取數據時強制關閉/崩潰

protected Void doInBackground(Void... params) { 

    try { 

      /** Handling XML */ 
      spf = SAXParserFactory.newInstance(); 
      sp = spf.newSAXParser(); 
      xr = sp.getXMLReader(); 

      String response = doGet("http://www.abc.com"); 

        myXMLHandler = new MyXMLHandler(); 
      xr.setContentHandler(myXMLHandler); 
      //xr.parse(new InputSource(sourceUrl.openStream())); 
      xr.parse(new InputSource(new StringReader(response))); 

     } 
     catch (UnknownHostException e) { 
      e.printStackTrace(); 
      } 
     catch (NullPointerException e) { 
      e.printStackTrace(); 
     } catch (IOException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } catch (SAXException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } catch (ParserConfigurationException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 

     markerssList = MyXMLHandler.markerList; 
     mapOverlays_results =mapView.getOverlays(); 
     mapOverlays_results.remove(itemizedOverlay); 

      if(markerssList.getLatitude().size()>0) 
     { 
      Drawable drawable = HSLoc_tabActivity.this.getResources().getDrawable(R.drawable.flag); 
      itemizedOverlay = new CustomItemizedOverlay(drawable, HSLoc_tabActivity.this); 
      for (int i = 0; i < markerssList.getLatitude().size(); i++) { 

       ss1 = markerssList.getLatitude().get(i); 
       ss2 = markerssList.getLongitude().get(i); 
       ss3 = markerssList.getName().get(i); 
       ss4 = markerssList.getAddress().get(i); 
       ss5 = markerssList.getCity().get(i); 
       markerssList.getPostal_code().get(i); 

       try 
       { 
        p = new GeoPoint((int) (Double.parseDouble(ss1) * 1E6), 
          (int) (Double.parseDouble(ss2) * 1E6)); 
        overlayItem = new OverlayItem(p, " ", 
          ""+ss3+"\n"+ss4+"\n"+ss5); 
        itemizedOverlay.addOverlay(overlayItem); 

       } 
       catch(NullPointerException e1) 
       { 

       } 

      } 
      mapOverlays_results.add(itemizedOverlay); 
      //mapView.getOverlays().add(itemizedOverlay); 
      mapController = mapView.getController(); 
      mapController.animateTo(p); 
      mapController.setZoom(12); 
      mapView.postInvalidate(); 

}

代碼工作fine..but在低網絡應用程序強制關閉,我得到了他以下錯誤:

 01-02 13:41:29.222: WARN/System.err(6562): java.net.UnknownHostException: www.abc.com.au 
    01-02 13:41:29.222: WARN/System.err(6562):  at java.net.InetAddress.lookupHostByName(InetAddress.java:509) 
    01-02 13:41:29.232: WARN/System.err(6562):  at java.net.InetAddress.getAllByNameImpl(InetAddress.java:297) 
    01-02 13:41:29.232: WARN/System.err(6562):  at java.net.InetAddress.getAllByName(InetAddress.java:256) 
    01-02 13:41:29.232: WARN/System.err(6562):  at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:136) 
    01-02 13:41:29.232: WARN/System.err(6562):  at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164) 
    01-02 13:41:29.232: WARN/System.err(6562):  at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119) 
    01-02 13:41:29.232: WARN/System.err(6562):  at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:359 

) 

01-02 13:41:29.312: ERROR/AndroidRuntime(6562): FATAL EXCEPTION: AsyncTask #2 
01-02 13:41:29.312: ERROR/AndroidRuntime(6562): java.lang.RuntimeException: An error occured while executing doInBackground() 
01-02 13:41:29.312: ERROR/AndroidRuntime(6562):  at android.os.AsyncTask$3.done(AsyncTask.java:200) 
01-02 13:41:29.312: ERROR/AndroidRuntime(6562):  at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:274) 
01-02 13:41:29.312: ERROR/AndroidRuntime(6562):  at java.util.concurrent.FutureTask.setException(FutureTask.java:125) 
01-02 13:41:29.312: ERROR/AndroidRuntime(6562):  at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:308) 
01-02 13:41:29.312: ERROR/AndroidRuntime(6562):  at java.util.concurrent.FutureTask.run(FutureTask.java:138) 
01-02 13:41:29.312: ERROR/AndroidRuntime(6562):  at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1088) 
01-02 13:41:29.312: ERROR/AndroidRuntime(6562):  at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:581) 
01-02 13:41:29.312: ERROR/AndroidRuntime(6562):  at java.lang.Thread.run(Thread.java:1027) 
01-02 13:41:29.312: ERROR/AndroidRuntime(6562): Caused by: java.lang.NullPointerException 
01-02 13:41:29.312: ERROR/AndroidRuntime(6562):  at com.app.LocatorApp.HSLoc_tabActivity$backgroundLoadResults.doInBackground(HSLoc_tabActivity.java:545) 
01-02 13:41:29.312: ERROR/AndroidRuntime(6562):  at com.app.LocatorApp.HSLoc_tabActivity$backgroundLoadResults.doInBackground(HSLoc_tabActivity.java:1) 

如何處理這些錯誤?

+0

在解析XML之前,您是否試圖查看響應?你真的明白嗎? – 2012-01-02 08:28:22

回答

1

當您在建立連接時遇到異常情況時,可能在繼續後臺任務時沒有多大意義。您的catch塊應該可能返回某種錯誤指示。相反,他們通過並且doInBackground繼續執行,導致NullPointerException。

+0

這意味着我應該取消catch塊中的任務? – 2012-01-02 08:36:40

+0

@nibha - 這就是我的建議。如果連接失敗,繼續執行任務的邏輯是什麼? – 2012-01-02 16:23:59