If you’ve ever wanted to access weather reports from your desktop applications, widgets, or web applications, I’ve got some source code that will make it easier than ever to access this weather data from a Java API. There are a couple of ways to get access to weather data:
- You can access weather.com’s XML data feed (version 2.0). By passing some key-value pairs to this service, using HTTP GET, you can get a set of XML documents that contain the reponse to your query. You have to pass the location and the kind of weather forecast data that you want to this service, and it will give you back an XML document. There’s also a service that allows you to lookup location Ids for addresses. Weather.com provides a DTD and user guide to help you parse the response from the service, and create requests, etc. It’s simple, but tedious to make the queries and parse the response; it would be much easier if a set of Java classes are provided.
- NOAA also provides a SOAP based web service that will give you detailed weather data, and it’s free for use by the public – our tax dollars are funding it :). However, you have to use SOAP and XSDs, and if you are comfortable with web services then it will give you what you need.
In this article, I will provide an overview of the source code that I’ve written to allow you to parse this weather information in Java. I’ve provided some classes that allow you to very easily query the weather.com XML data feed for current weather information that’s transformed into a set of Javabeans that contain weather forecast information that you requested.
The classes that are provided allow you to get weather information by simply using a WeatherGateway class and invoking methods on it to:
- Given a city name, resolve it to a weather.com location Id (zip codes in the US can be used as location Ids). A city name can resolve to more than one location Id, and if this is the case, a list of location Ids is provided to you to choose from (in a SearchLocations Javabean class).
- Given a location Id, you can look up the weather for this location. You can choose to get current conditions, or detailed forecast reports for days to come, you can configure all this by calling different methods and passing different parameters. The response is a WeatherReport Javabean class that holds all the weather data that you asked for.
- To see how to use the code, a sample class called TestWeatherGateway is provided, which allows you to test all the various methods that are available and see what the response is. Download the code and run this class and see the output on your command prompt/console, and it you will see the structure of this bean. You can get more details on it by reading the weather.com Guide on their XML data structure (which you can download once you sign up with them, instructions provided in the next section)
- To make your life easy, all the error codes generated by the weather.com service are also parsed and thrown as Exceptions (WeatherError exception class) when you use the WeatherGateway class. All the weather.com errors are encapsulated in WeatherError.ErrorTypes, and any networking issues are reported as an IOException.
- Also, the WeatherGateway class caches the responses of URIs that weather.com responded to without any errors, as weather.com has some guidelines around how often you should query these services. You can read more about this in the Weather.com XML data feed guide, once you sign up on their website (instructions are provided in the next section).