首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何为天气预报网站找到API?

如何为天气预报网站找到API?
EN

Stack Overflow用户
提问于 2013-05-16 10:51:05
回答 6查看 14K关注 0票数 4

我制作了一个java应用程序,向用户发出天气通知。我使用了雅虎提供的雅虎气象API,就像这个链接:

http://weather.yahooapis.com/forecastrss?w=2502265

我所要做的就是更改URL中的八个编号代码,以改变这个城市。

这是完美的,但我现在面临着两个问题:

首先,我想在我的应用程序中实现很多天气预报源,而不仅仅是雅虎天气,我在任何其他天气预报网站上都找不到类似的服务。

第二,我想获得雅虎天气中所有城市的代码,我肯定不会要求用户输入他的城市代码,而是输入他的城市名称,我将与代码匹配。

下面是与我一起使用java的代码:

返回XML文件的代码:

代码语言:javascript
复制
package search;

import java.io.IOException;
import java.io.InputStream;
import java.io.StringWriter;
import java.net.URL;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.w3c.dom.Document;

    public class Process {

        public static void main(String[] args) throws IOException {

            Display disp = new Display();

            Document doc = generateXML("1940345");
            disp.getConditions(doc);

        }

        public static Document generateXML(String code) throws IOException {

            String url = null;
            String XmlData = null;

            // creating the URL
            url = "http://weather.yahooapis.com/forecastrss?w=" + code;
            URL xmlUrl = new URL(url);
            InputStream in = xmlUrl.openStream();

            // parsing the XmlUrl
            Document doc = parse(in);

            return doc;

        }

        public static Document parse(InputStream is) {
            Document doc = null;
            DocumentBuilderFactory domFactory;
            DocumentBuilder builder;

            try {
                domFactory = DocumentBuilderFactory.newInstance();
                domFactory.setValidating(false);
                domFactory.setNamespaceAware(false);
                builder = domFactory.newDocumentBuilder();

                doc = builder.parse(is);
            } catch (Exception ex) {
                System.err.println("unable to load XML: " + ex);
            }
            return doc;
        }
    }

显示城市温湿度的代码:

代码语言:javascript
复制
package search;

import java.io.File;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

public class Display {

    static void getConditions(Document doc) {

        String city = null;
        String unit = null;

        try {

            doc.getDocumentElement().normalize();

            NodeList nList = doc.getElementsByTagName("rss");

            for (int temp = 0; temp < nList.getLength(); temp++) {

                Node nNode = nList.item(temp);

                if (nNode.getNodeType() == Node.ELEMENT_NODE) {

                    Element eElement = (Element) nNode;

                    NodeList nl = eElement
                            .getElementsByTagName("yweather:location");

                    for (int tempr = 0; tempr < nl.getLength(); tempr++) {

                        Node n = nl.item(tempr);

                        if (nNode.getNodeType() == Node.ELEMENT_NODE) {

                            Element e = (Element) n;
                            city = e.getAttribute("city");
                            System.out.println("The City Is : " + city);

                        }
                    }

                    NodeList nl2 = eElement
                            .getElementsByTagName("yweather:units");

                    for (int tempr = 0; tempr < nl2.getLength(); tempr++) {

                        Node n2 = nl2.item(tempr);

                        if (nNode.getNodeType() == Node.ELEMENT_NODE) {

                            Element e2 = (Element) n2;
                            unit = e2.getAttribute("temperature");

                        }
                    }

                    NodeList nl3 = eElement
                            .getElementsByTagName("yweather:condition");

                    for (int tempr = 0; tempr < nl3.getLength(); tempr++) {

                        Node n3 = nl3.item(tempr);

                        if (nNode.getNodeType() == Node.ELEMENT_NODE) {

                            Element e3 = (Element) n3;
                            System.out.println("The Temperature In " + city
                                    + " Is : " + e3.getAttribute("temp") + " "
                                    + unit);
                        }
                    }

                    NodeList nl4 = eElement
                            .getElementsByTagName("yweather:atmosphere");

                    for (int tempr = 0; tempr < nl4.getLength(); tempr++) {

                        Node n4 = nl4.item(tempr);

                        if (nNode.getNodeType() == Node.ELEMENT_NODE) {

                            Element e4 = (Element) n4;
                            System.out.println("The Humidity In " + city
                                    + " Is : " + e4.getAttribute("humidity"));
                        }
                    }

                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
EN

回答 6

Stack Overflow用户

发布于 2013-05-20 21:19:04

您可以使用Metwit 气象api,只需通过纬度和经度。

如果您可以实现它们,客户端: 200请求/天(基于ip的节流)不需要认证。全球范围,JSON和REST兼容。您可以免费注册额外的API调用,如果您仍然需要它来调用服务器端,则基本计划相当便宜。

完全公开:我拥有这个API。

票数 2
EN

Stack Overflow用户

发布于 2013-05-16 10:55:25

看看这个讨论吧。这似乎是相关的:

https://stackoverflow.com/questions/4876800/is-there-an-international-weather-forecast-api-that-is-not-limited-for-non-comme

另外,在谷歌中键入“天气预报api”。有大量对API的引用,这些API支持多种气象服务。

票数 1
EN

Stack Overflow用户

发布于 2013-05-16 13:26:10

以下是通过提供的天气API列表:

https://temboo.com/library/keyword/weather/

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/16585479

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档