3

I am using the following website to find elevation:

http://gisdata.usgs.gov/xmlwebservices2/elevation_service.asmx/getElevation?X_Value=-78.85834070853889&Y_Value=43.869369104504585&Elevation_Units=METERS&Source_Layer=-1&Elevation_Only=true

This provides a results in the format of 93.7665481567383 in an XML format.

Anyone know of a quick and simple way of extracting this data for my android program?

I tried the following but I keep getting "null" as the output.

HttpURLConnection connection = null;
URL serverAddress = null;
serverAddress = new URL("http://gisdata.usgs.gov/xmlwebservices2/elevation_service.asmx/getElevation?X_Value=-78.85834070853889&Y_Value=43.869369104504585&Elevation_Units=METERS&Source_Layer=-1&Elevation_Only=true");

connection = null;
connection = (HttpURLConnection)serverAddress.openConnection();
connection.setRequestMethod("GET");
connection.setDoOutput(true);
connection.setReadTimeout(10000);

connection.connect();
BufferedReader rd  = new BufferedReader(new
InputStreamReader(connection.getInputStream()));
StringBuilder sb = new StringBuilder();

String line = null;
line = rd.readLine();
while ((line = rd.readLine()) != null)
{
    sb.append(line + '\n');
}

When i output sb.toString() i get Null

Any ideas?

2
  • Have you added <uses-permission android:name="android.permission.INTERNET" /> permission to the manifest? Commented Jan 24, 2012 at 19:49
  • yes I have added that to the manifest already. Still doesn't work Commented Jan 24, 2012 at 21:49

2 Answers 2

3

You made a small mistake:

the first rd.readLine() returns your <tag>number</tag> stuff but the while loop erase it with null again. Remove the first rd.readLine() call and it should work.

String line = null;
line = rd.readLine(); // you get the only line... remove it!
while ((line = rd.readLine()) != null) {
    sb.append(line + '\n');
}

To get your number, try to use something like this:

// line should contain your string...
line = line.substring(line.indexOf(">"), line.indexOf("</"));
Sign up to request clarification or add additional context in comments.

1 Comment

Great point, thanks for that. However, that did not resolve the issue I was able to trace the problem to the line InputStreamReader(connection.getInputStream())); the error I get is: D/gps ( 2908): java.io.FileNotFoundException: gisdata.usgs.gov/xmlwebservices2/elevation_service.asmx/… 89&Y_Value=43.869369104504585&Elevation_Units=METERS&Source_Layer=-1&Elevation_Only=true Any ideas?
1

Without actually trying this myself, I recommend trying to parse the XML as opposed to trying to read it in from a Buffer. To do this in great detail, you will need to know the XML tree structure, because you will be reading in based on Nodes. For example:

// Data members
private URL URL;
private InputStream stream;
private DocumentBuilder builder;
private Document document;
private Element root;
private NodeList nodeList;

URL = new URL(url); // The URL of the site you posted goes here.
stream = URL.openStream();

// Set up and initialize the document.
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
dbf.setIgnoringElementContentWhitespace(true);
builder = dbf.newDocumentBuilder();
document = builder.parse(stream);
document.getDocumentElement().normalize();

root = document.getDocumentElement();
nodeList = root.getChildNodes();


// The number of calls to 'getFirstChild()' will vary with the complexity of
// your XML tree. Also, 'i' could vary as well, depending on which Node off
// of the root you want to access.
String number = nodeList.item(i).getFirstChild().getFirstChild().getNodeValue();

That may seem very confusing, but let me give you an example in XML.

<Building>
    <name>Train Station</name>
    <address>8 Main Street</address>
    <color>Blue</color>
</Building>
<Building>
    <name>Drugstore</name>
    <address>14 Howard Boulevard</address>
    <color>Yellow</color>
</Building>

Each building would represent a different value passed as a parameter to '.item(i)'. To access information about the first Building, pass a value of 0. The children of Building are accessed using the '.getFirstChild()' method, but that only returns the Node. If you wanted the text "Drugstore", you would have to traverse the XML tree to the Data of the First Child of the Second Item, eg. nodeList.item(1).getFirstChild().getNodeValue();

I hope that helped!!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.