简体   繁体   中英

Getting a null value when attempting to get KML node value from phone, but not on the emulator with android

I am currently designing an application that needs to plot a route on a MapView. For this to function correctly I need to get data from a KML document that i get from:

http://maps.google.com/maps?f=d&hl=en&saddr=-33.882993,18.63486&daddr=-33.870162,18.657837&ie=UTF8&0&om=0&output=kml

I have created a test project to ensure that this data is received correctly. The problem is that it runs perfectly on my emulator, but not on the actual android phone.

The following piece of code starts a thread when a button is clicked, getting an input stream response (KML).

public void onClick(View v) {
        if (v.getId()== R.id.btn1)
        {   
              new Thread() {
                 @Override
                 public void run() {
                    String url = "http://maps.google.com/maps?f=d&hl=en&saddr=-33.882993,18.63486&daddr=-33.870162,18.657837&ie=UTF8&0&om=0&output=kml";
                    InputStream is = getConnection(url);

                    mRoad = RouteProvider.getRoute(is);
                    mHandler.sendEmptyMessage(0);
                 }
             }.start(); 
        }   

}

private InputStream getConnection(String url) {
    InputStream is = null;
    try {
           HttpURLConnection conn = (HttpURLConnection)new URL(url).openConnection();
            conn.connect();
            is = conn.getInputStream();
            conn = null;
    } catch (MalformedURLException e) {
            e.printStackTrace();
    } catch (IOException e) {
            e.printStackTrace();
    }
     return is;
    }

For the moment, all getRoute() needs to do is give the contents of the "LineString" element inside the KML Document. These contents are a list of coordinates that can be used to draw a route.

public class RouteProvider {
    /** Gets data from KML response **/
    public static Road getRoute(InputStream is) {
        Road road = new Road();

        try 
        {
            Document xmlDoc = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(is);
            NodeList a = xmlDoc.getElementsByTagName("LineString");
            System.out.println(a.item(0).getTextContent());
            road.mName = a.item(0).getTextContent();


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


        return road;
      }

On the emulator I am running the correct value is displayed within road.mName, which is a list of coordinates. On a Android phone it displays null?

I am building for Android 2.3.3 on eclipse and I used a Samsung Galaxy S2 for testing.

Did not need to use KML after all. Just used google maps to supply xml format. Much easier. https://developers.google.com/maps/documentation/directions/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM