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

问题描述 投票:2回答:2

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

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

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

This is the documentation I found

weather yahoo-weather-api
2个回答
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找到许多其他的替代方法


0
投票

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

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

© www.soinside.com 2019 - 2024. All rights reserved.