Search code examples
javawunderground

Fetching weather data using java and the wunderground api


I'm trying to fetch some weather data using java. I am using the following java api for fetching the data from wunderground.com

https://code.google.com/p/wunderground-core/

The example code they give on their website works okay for (Dortmund in Germany). However when I change the key from dortmund to Boston in the U.S.A, I get null pointer errors. Any idea what I could be doing wrong? Please try it and leave comments/advice. Thanks!

Code:

import de.mbenning.weather.wunderground.api.domain.DataSet;
import de.mbenning.weather.wunderground.api.domain.WeatherStation;
import de.mbenning.weather.wunderground.api.domain.WeatherStations;
import de.mbenning.weather.wunderground.impl.services.HttpDataReaderService;


public class weather {

    public static void main(String[] args)
    {

    // create a instance of a wunderground data reader
    HttpDataReaderService dataReader = new HttpDataReaderService();

    // select a wunderground weather station (ID "INORDRHE72" = Dortmund-Mengede)
    WeatherStation weatherStation = WeatherStations.ALL.get("INORDRHE72");
    // KMABOSTO22 is the ID for Boston South end
    //WeatherStation weatherStation = WeatherStations.ALL.get("KMABOSTO32");

    // set selected weather station to data reader
    dataReader.setWeatherStation(weatherStation);

    // get current (last) weather data set from selected station
    DataSet current = dataReader.getCurrentData();

    // print selected weather station ID
    System.out.println(weatherStation.getStationId());

    // print city, state and country of weather station
    System.out.println(weatherStation.getCity() + " " + weatherStation.getState() + " " + weatherStation.getCountry());

    //`enter code here` print datetime of measure and temperature ...
    System.out.println(current.getDateTime() + " " + current.getTemperature());
    }

}

Solution

  • Check out the source code of the Wunderground API.

    svn checkout http://wunderground-core.googlecode.com/svn/trunk/ wunderground-core-read-only
    

    In the package de.mbenning.weather.wunderground.api.domain there is a class called WeatherStations. There you will find the content of all weather stations you can call in your code. Right now there are only a few ones:

    public static final Map<String, WeatherStation> ALL = new HashMap<String, WeatherStation>();
    static {
        ALL.put("INRWKLEV2", INRWKLEV2_KLEVE);
        ALL.put("INORDRHE110", INORDRHE110_GOCH);
        ALL.put("IDRENTHE48", IDRENTHE48_COEVORDEN);
        ALL.put("IZEELAND13", IZEELAND13_GOES);
        ALL.put("INORDRHE72", INORDRHE72_DORTMUND);
        ALL.put("INOORDBR35", INOORDBR35_BOXMEER);
    }; 
    

    All others won't work.