开发者

Unable to handle error in google map parsing

I am able to parse google map according to location name. Now my problem is when i enter a wrong location in my editbox and press enter button it crashes. I want: when no location is found then it will show a message "No Location found Enter location correctly" instead of crash. How can i do this? My parsing code is here:

            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();
            } 

And my Log cat is:

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): 开发者_Go百科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


The clue is the line:

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

Looking at your code, you have an exception handler which catches SAXException:

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

All you need to do is change the body of this catch block to do whatever you like.

However, it might be better still to check the return code of your HTTP connection before trying to parse the response:

        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
        }


As you can see

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

this link return a 0KB file.

So you should not let the parser use this. Check how much of bytes are available for download from the InputStream urlConnection.getInputStream()

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜