2011-07-05 53 views
-1

我能夠根據位置名稱解析谷歌地圖。現在我的問題是,當我在我的編輯框中輸入一個錯誤的位置,然後按回車鍵崩潰。我想要:當找不到位置時,它會顯示一條消息「找不到位置正確輸入位置」而不是崩潰。我怎樣才能做到這一點?我的解析代碼是在這裏:無法處理谷歌地圖解析中的錯誤

  StringBuilder urlString = new StringBuilder(); 
      urlString.append("http://maps.google.com/maps?q="); 
      urlString.append(enter.getText().toString());//from 
      urlString.append(",dhaka"); 
      urlString.append("&ie=UTF8&0&om=0&output=kml"); 
      Log.d("xxx","URL="+urlString.toString()); 
      // get the kml (XML) doc. And parse it to get the coordinates(direction route). 
      Document doc = null; 
      HttpURLConnection urlConnection= null; 
      URL url = null; 
      try 
      { 
      url = new URL(urlString.toString()); 
      urlConnection=(HttpURLConnection)url.openConnection(); 
      urlConnection.setRequestMethod("GET"); 
      urlConnection.setDoOutput(true); 
      urlConnection.setDoInput(true); 
      urlConnection.connect(); 

      DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); 
      DocumentBuilder db = dbf.newDocumentBuilder(); 
      doc = db.parse(urlConnection.getInputStream()); 
      if(doc.getElementsByTagName("Point").getLength()>0) 
      { 
       String path = doc.getElementsByTagName("Point").item(0).getFirstChild().getFirstChild().getNodeValue() ; 
       Log.d("xxx","path="+ path); 
       String [] pairs = path.split(" "); 
       lngLat = pairs[0].split(","); 

      } 

      } 
      catch (MalformedURLException e) 
      { 
         e.printStackTrace(); 

      } 
      catch (IOException e) 
      { 
      e.printStackTrace(); 

      catch (ParserConfigurationException e) 
      { 
       e.printStackTrace(); 

      } 
      catch (SAXException e) 
      { 
          e.printStackTrace(); 
      } 

我的日誌貓:

07-05 13:33:42.648: DEBUG/AndroidRuntime(439): >>>>>>>>>>>>>> AndroidRuntime START <<<<<<<<<<<<<< 

07-05 13:33:42.648: DEBUG/AndroidRuntime(439): CheckJNI is ON 

07-05 13:33:42.908: DEBUG/AndroidRuntime(439): --- registering native functions --- 

07-05 13:33:43.798: DEBUG/AndroidRuntime(439): Shutting down VM 

07-05 13:33:43.808: DEBUG/dalvikvm(439): Debugger has detached; object registry had 1 entries 

07-05 13:33:43.819: INFO/AndroidRuntime(439): NOTE: attach of thread 'Binder Thread #3' failed 

07-05 13:33:44.449: DEBUG/AndroidRuntime(447): >>>>>>>>>>>>>> AndroidRuntime START <<<<<<<<<<<<<< 

07-05 13:33:44.449: DEBUG/AndroidRuntime(447): CheckJNI is ON 

07-05 13:33:44.678: DEBUG/AndroidRuntime(447): --- registering native functions --- 

07-05 13:33:45.498: DEBUG/AndroidRuntime(447): Shutting down VM 

07-05 13:33:45.508: DEBUG/dalvikvm(447): Debugger has detached; object registry had 1 entries 

07-05 13:33:45.537: INFO/AndroidRuntime(447): NOTE: attach of thread 'Binder Thread #3' failed 

07-05 13:33:46.157: DEBUG/AndroidRuntime(455): >>>>>>>>>>>>>> AndroidRuntime START <<<<<<<<<<<<<< 

07-05 13:33:46.157: DEBUG/AndroidRuntime(455): CheckJNI is ON 

07-05 13:33:46.388: DEBUG/AndroidRuntime(455): --- registering native functions --- 

07-05 13:33:47.279: INFO/ActivityManager(77): Force stopping package org.example.SearchLocation uid=10042 

07-05 13:33:47.279: INFO/ActivityManager(77): Starting activity: Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10000000 cmp=org.example.SearchLocation/.SearchLocation } 

07-05 13:33:47.489: DEBUG/AndroidRuntime(455): Shutting down VM 

07-05 13:33:47.537: DEBUG/dalvikvm(455): Debugger has detached; object registry had 1 entries 

07-05 13:33:47.608: INFO/AndroidRuntime(455): NOTE: attach of thread 'Binder Thread #3' failed 

07-05 13:33:47.639: INFO/ActivityManager(77): Start proc org.example.SearchLocation for activity org.example.SearchLocation/.SearchLocation: pid=464 uid=10042 gids={3003} 

07-05 13:33:48.019: DEBUG/dalvikvm(32): GC_EXPLICIT freed 285 objects/10848 bytes in 334ms 

07-05 13:33:48.248: DEBUG/dalvikvm(32): GC_EXPLICIT freed 47 objects/2064 bytes in 160ms 

07-05 13:33:48.457: WARN/ActivityThread(464): Application org.example.SearchLocation is waiting for the debugger on port 8100... 

07-05 13:33:48.548: INFO/System.out(464): Sending WAIT chunk 

07-05 13:33:48.559: INFO/dalvikvm(464): Debugger is active 

07-05 13:33:48.628: INFO/System.out(464): Debugger has connected 

07-05 13:33:48.677: INFO/System.out(464): waiting for debugger to settle... 

07-05 13:33:48.819: DEBUG/dalvikvm(32): GC_EXPLICIT freed 2 objects/64 bytes in 557ms 

07-05 13:33:48.877: INFO/System.out(464): waiting for debugger to settle... 
07-05 13:33:49.077: INFO/System.out(464): waiting for debugger to settle... 

07-05 13:33:49.287: INFO/System.out(464): waiting for debugger to settle... 

07-05 13:33:49.489: INFO/System.out(464): waiting for debugger to settle... 

07-05 13:33:49.688: INFO/System.out(464): waiting for debugger to settle... 

07-05 13:33:49.887: INFO/System.out(464): waiting for debugger to settle... 

07-05 13:33:50.097: INFO/System.out(464): waiting for debugger to settle... 

07-05 13:33:50.297: INFO/System.out(464): waiting for debugger to settle... 

07-05 13:33:50.497: INFO/System.out(464): waiting for debugger to settle... 

07-05 13:33:50.708: INFO/System.out(464): waiting for debugger to settle... 

07-05 13:33:50.917: INFO/System.out(464): waiting for debugger to settle... 

07-05 13:33:51.196: INFO/System.out(464): waiting for debugger to settle... 

07-05 13:33:51.459: INFO/System.out(464): debugger has settled (1485) 

07-05 13:33:53.728: WARN/GpsLocationProvider(77): Duplicate add listener for uid 10042 

07-05 13:33:54.137: INFO/MapActivity(464): Handling network change notification:CONNECTED 

07-05 13:33:54.137: ERROR/MapActivity(464): Couldn't get connection factory client 

07-05 13:33:54.457: INFO/ActivityManager(77): Displayed activity org.example.SearchLocation/.SearchLocation: 6966 ms (total 6966 ms) 

07-05 13:33:59.750: DEBUG/dalvikvm(142): GC_EXPLICIT freed 230 objects/10528 bytes in 144ms 

07-05 13:34:28.398: INFO/ActivityManager(77): Starting activity: Intent { cmp=org.example.SearchLocation/.SearchByAddress } 

07-05 13:34:29.359: DEBUG/dalvikvm(464): GC_EXTERNAL_ALLOC freed 2877 objects/189920 bytes in 98ms 

07-05 13:34:31.198: INFO/ActivityManager(77): Displayed activity org.example.SearchLocation/.SearchByAddress: 2657 ms (total 2657 ms) 

07-05 13:34:34.128: WARN/KeyCharacterMap(464): No keyboard for id 0 

07-05 13:34:34.128: WARN/KeyCharacterMap(464): Using default keymap: /system/usr/keychars/qwerty.kcm.bin 

07-05 13:34:34.569: DEBUG/dalvikvm(135): GC_EXTERNAL_ALLOC freed 3307 objects/214544 bytes in 244ms 

07-05 13:34:37.518: DEBUG/dalvikvm(208): GC_EXPLICIT freed 1014 objects/54176 bytes in 1144ms 

07-05 13:34:39.919: DEBUG/xxx(464): URL=http://maps.google.com/maps?q=gdgrgdgd,dhaka&ie=UTF8&0&om=0&output=kml 

07-05 13:34:42.017: WARN/System.err(464): org.xml.sax.SAXParseException: Unexpected end of document 

07-05 13:34:42.028: WARN/System.err(464):  at org.apache.harmony.xml.parsers.DocumentBuilderImpl.parse(DocumentBuilderImpl.java:131) 

07-05 13:34:42.028: WARN/System.err(464):  at javax.xml.parsers.DocumentBuilder.parse(DocumentBuilder.java:110) 

07-05 13:34:42.039: WARN/System.err(464):  at org.example.SearchLocation.SearchByAddress$2.onClick(SearchByAddress.java:227) 

07-05 13:34:42.048: WARN/System.err(464):  at android.view.View.performClick(View.java:2408) 

07-05 13:34:42.057: WARN/System.err(464):  at android.view.View$PerformClick.run(View.java:8816) 

07-05 13:34:42.057: WARN/System.err(464):  at android.os.Handler.handleCallback(Handler.java:587) 

07-05 13:34:42.078: WARN/System.err(464):  at android.os.Handler.dispatchMessage(Handler.java:92) 

0 7-05 13:34:42.078: WARN/System.err(464):  at android.os.Looper.loop(Looper.java:123) 

07-05 13:34:42.087: WARN/System.err(464):  at android.app.ActivityThread.main(ActivityThread.java:4627) 

07-05 13:34:42.098: WARN/System.err(464):  at java.lang.reflect.Method.invokeNative(Native Method) 

07-05 13:34:42.109: WARN/System.err(464):  at java.lang.reflect.Method.invoke(Method.java:521) 

07-05 13:34:42.109: WARN/System.err(464):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868) 

07-05 13:34:42.119: WARN/System.err(464):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626) 

07-05 13:34:42.127: WARN/System.err(464):  at dalvik.system.NativeStart.main(Native Method) 

07-05 13:35:17.350: DEBUG/SntpClient(77): request time failed: java.net.SocketException: Address family not supported by protocol 
+0

顯然會顯示您的Logcat輸出。 – Reno

回答

1

線索是線:

07-05 13:34:42.017: WARN/System.err(464): org.xml.sax.SAXParseException: Unexpected end of document 

看你的代碼,你有一個異常處理程序,其捕獲SAXException

 catch (SAXException e) 
     { 
         e.printStackTrace(); 
     } 

所有你需要做的就是改變這個catch塊的主體去做任何你喜歡的事情。

但是,它可能是最好還要檢查你的HTTP連接的返回代碼試圖解析響應之前:

 urlConnection=(HttpURLConnection)url.openConnection(); 
     urlConnection.setRequestMethod("GET"); 
     urlConnection.setDoOutput(true); 
     urlConnection.setDoInput(true); 
     urlConnection.connect(); 

     if(urlConnection.getResponseCode() == HttpURLConnection.HTTP_OK) { 
      DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); 
      DocumentBuilder db = dbf.newDocumentBuilder(); 
      doc = db.parse(urlConnection.getInputStream()); 
      if(doc.getElementsByTagName("Point").getLength()>0) 
      { 
       String path = doc.getElementsByTagName("Point").item(0).getFirstChild().getFirstChild().getNodeValue() ; 
       Log.d("xxx","path="+ path); 
       String [] pairs = path.split(" "); 
       lngLat = pairs[0].split(","); 
      } 
     } 
     else { 
      // Report error 
     } 
+0

我已經試過用你的方式,但它崩潰 –

+0

崩潰如何?任何錯誤消息?從logcat堆棧跟蹤? –

+0

消息是:「應用程序ATMTracker(進程org.example.SearchLocation)意外停止,請重試」。 –

0

正如你可以看到

http://maps.google.com/maps?q=gdgrgdgd,dhaka&ie=UTF8&0&om=0&output=kml

此鏈接返回一個0KB文件。

所以你不應該讓解析器使用這個。檢查InputStream中有多少字節可供下載urlConnection.getInputStream()

InuptStream input = urlConnection.getInputStream() 
if(input.available() > 0) 
{ 
    // parse 
} 
else 
{ 
    // "No Location found enter the location correctly" 
} 
+0

我已經檢查null,但它崩潰 –