如何拨打雅虎小时天气预报API?

时间:2013-04-25 11:13:44

标签: weather yahoo-weather-api

我发现雅虎天气预报最有帮助。

我能够从雅虎获得每小时天气请求here

如何使用对http://weather.yahooapis.com/forecastrss?w=2502265的Yahoo API调用,针对上述每小时天气预报发出API请求?

This is the documentation I found

2 个答案:

答案 0 :(得分:0)

您可以使用您想要使用的编程语言的REST API。我将提供 Java 示例。 (类似的东西也适用于其他语言..)'

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();
    }
  }

还有更多方法可以做同样的事情......您可以在http://alvinalexander.com/java/java-apache-httpclient-restful-client-examples

找到许多其他替代方法

答案 1 :(得分:0)

雅虎天气Api似乎不支持每小时预测,只有一些参数可以控制,如(woeid)或(lat,long)和温度单位(u或f)中的位置,请参阅{ {3}}用于雅虎查询语言。

您可以使用其他api的here获取每小时的详细信息。