Search code examples
weatheryahoo-weather-api

How do I make a call to the Yahoo hourly weather forecast API?


I have found yahoo weather forcast most helpful.

I'm able to get an hourly weather request here from Yahoo.

How can I make an API request for the above hourly weather report using an Yahoo API call to http://weather.yahooapis.com/forecastrss?w=2502265?

This is the documentation I found.


Solution

  • You can do using the REST API's of the programming language you want to use.. I will give Java example. (Similar thing applies to other languages too.. )'

    package tests;
    
    import org.apache.http.*;
    import org.apache.http.client.methods.HttpGet;
    import org.apache.http.impl.client.DefaultHttpClient;
    import org.apache.http.util.EntityUtils;
    
    /**
     * A simple Java REST GET example using the Apache HTTP library.
     * This executes a call against the Yahoo Weather API service, which is
     * actually an RSS service (http://developer.yahoo.com/weather/).
     * 
     * Try this Twitter API URL for another example (it returns JSON results):
     * http://search.twitter.com/search.json?q=%40apple
     * (see this url for more twitter info: https://dev.twitter.com/docs/using-search)
     * 
     * Apache HttpClient: http://hc.apache.org/httpclient-3.x/
     *
     */
    public class ApacheHttpRestClient1 {
    
      public static void main(String[] args) {
        DefaultHttpClient httpclient = new DefaultHttpClient();
        try {
          // specify the host, protocol, and port
          HttpHost target = new HttpHost("weather.yahooapis.com", 80, "http");
    
          // specify the get request
          HttpGet getRequest = new HttpGet("/forecastrss?p=80020&u=f");
    
          System.out.println("executing request to " + target);
    
          HttpResponse httpResponse = httpclient.execute(target, getRequest);
          HttpEntity entity = httpResponse.getEntity();
    
          System.out.println("----------------------------------------");
          System.out.println(httpResponse.getStatusLine());
          Header[] headers = httpResponse.getAllHeaders();
          for (int i = 0; i < headers.length; i++) {
            System.out.println(headers[i]);
          }
          System.out.println("----------------------------------------");
    
          if (entity != null) {
            System.out.println(EntityUtils.toString(entity));
          }
    
        } catch (Exception e) {
          e.printStackTrace();
        } finally {
          // When HttpClient instance is no longer needed,
          // shut down the connection manager to ensure
          // immediate deallocation of all system resources
          httpclient.getConnectionManager().shutdown();
        }
      }
    

    There are more ways to do the same... you can find many other alternate ways at http://alvinalexander.com/java/java-apache-httpclient-restful-client-examples