1

Currently I have a picture of a map in Android Studio, and I was wondering how I'd be able to pull information from wunderground and display it on top of the map in each location.

This is the website with some sample code.

Mr. Concolato
  • 2,224
  • 5
  • 24
  • 44
Lufval
  • 41
  • 3

1 Answers1

1

That would depend on the type of data. Concider an example that pulls eathquake data location from a JSON feed.

try {
        JSONObject object = (JSONObject) new JSONTokener(JSONResponse)
                .nextValue();
        JSONArray earthquakes = object.getJSONArray("features");

        for (int i = 0; i < earthquakes.length(); i++) {
            JSONObject tmp = (JSONObject) earthquakes.get(i);
            //Log.i("JSON: ",tmp.toString());

            JSONObject geometry = tmp.getJSONObject("geometry");
            JSONArray coords = geometry.getJSONArray("coordinates");
            JSONObject properties = tmp.getJSONObject("properties");

            //Log.i("Data", "Coords:"+coords.getString(0) + " "+ coords.getString(1)+"\n Place:"+properties.getString("place")+ " Mag:"+properties.getString("mag"));

            if(coords.getString(0) != "" && coords.getString(1) != ""){
                result.add(new EarthQuakeRec(
                        Float.parseFloat(coords.getString(1)),//Lat
                        Float.parseFloat(coords.getString(0)),//Long
                        Float.parseFloat(properties.getString("mag")),//Magnitude
                        properties.getString("place")
                    )
                );
            }           
        }
    } catch (JSONException e) {
        e.printStackTrace();
    }

And the JSON structure looks something like this:

{
"id": "nc72241526",
"type": "Feature",
"geometry": {
    "type": "Point",
    "coordinates": [
        -122.0102,
        37.6053,
        6
    ]
},
"properties": {
    "detail": "http://earthquake.usgs.gov/earthquakes/feed/v1.0/detail/nc72241526.geojson",
    "type": "earthquake",
    "net": "nc",
    "tsunami": null,
    "sources": ",nc,",
    "title": "M 3.0 - 1km NE of Union City, California",
    "time": 1403371328500,
    "updated": 1403374699020,
    "mag": 3,
    "types": ",dyfi,focal-mechanism,general-link,geoserve,nearby-cities,origin,phase-data,scitech-link,tectonic-summary,",
    "place": "1km NE of Union City, California",
    "status": "AUTOMATIC",
    "ids": ",nc72241526,",
    "alert": null,
    "rms": 0.17,
    "code": "72241526",
    "url": "http://earthquake.usgs.gov/earthquakes/eventpage/nc72241526",
    "magType": "Md",
    "mmi": null,
    "cdi": 3.8,
    "tz": -420,
    "felt": 319,
    "nst": 75,
    "dmin": 0.03593261,
    "gap": 25.2,
    "sig": 260
}

}

Then you would start with the JSONObject class and drill down to the data points of interest. Use the Log class to help you trouble shoot and gauge your progress in traversing your data structure. In my example, it is commented out.

For XML, you can try something to the tune of this:

        try {

        // Create the Pull Parser
        XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
        XmlPullParser xpp = factory.newPullParser();

        // Set the Parser's input to be the XML document in the HTTP Response
        xpp.setInput(new InputStreamReader(response.getEntity()
                .getContent()));

        // Get the first Parser event and start iterating over the XML document 
        int eventType = xpp.getEventType();

        while (eventType != XmlPullParser.END_DOCUMENT) {

            if (eventType == XmlPullParser.START_TAG) {
                startTag(xpp.getName());
            } else if (eventType == XmlPullParser.END_TAG) {
                endTag(xpp.getName());
            } else if (eventType == XmlPullParser.TEXT) {
                text(xpp.getText());
            }
            eventType = xpp.next();
        }
        return mResults;
    } catch (XmlPullParserException e) {
        e.printStackTrace();
    }

It is a lot to throw at you, but you did not give much specificity. Hope that helps. ;)

Mr. Concolato
  • 2,224
  • 5
  • 24
  • 44
  • I apologize for the lack of specificity. What I was trying to pull was what the temperature is at that exact moment and weather condition(sunny, foggy, etc). – Lufval Jul 11 '14 at 22:43
  • Not sure what you mean by what format (sorry, very new). – Lufval Jul 11 '14 at 22:47
  • @Lufval Can you add a link to the URL that you are trying to pull the data from or add a snippet of the API code to your question. Not much to go on here. – Mr. Concolato Jul 11 '14 at 22:49
  • This is the website with some sample code: http://www.wunderground.com/weather/api/d/docs?d=resources/code-samples&MR=1 – Lufval Jul 11 '14 at 22:50
  • @Lufval It is a JSON API feed that is returning a JSON structure. Do you see the Cedar_Rapids.json example? What I have posted should help you. – Mr. Concolato Jul 11 '14 at 22:54