关于雅虎 — 英文天气的API 解析

本文详细介绍了如何使用Yahoo提供的API来获取天气预报信息,并解释了如何通过城市名称查询对应的城市ID。包括解析XML数据、获取城市代码的过程及实现代码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

官方提供api地址:

   http://developer.yahoo.com/yql/guide/running-chapt.html       
   http://developer.yahoo.com/weather/#terms

本例提供解析代码。api数据格式以上地址可查看

public void paserxml() throws UnsupportedEncodingException {
        String cityCode = getCityCode("shenzhen");
        String str = "http://weather.yahooapis.com/forecastrss?w="+cityCode+"&u=c";
        try {    
             domfac = DocumentBuilderFactory
                     .newInstance();
             dombuilder = domfac.newDocumentBuilder();
             
            // NodeList channels;
             URL ur = new URL(str);
             HttpURLConnection  urlConnection = (HttpURLConnection) ur.openConnection();//打开一个连接  
             int code1 =urlConnection.getResponseCode();
             Log.e("liyongjun", "code = "+code1);
             doc = dombuilder.parse(urlConnection.getInputStream());
             
             root = doc.getDocumentElement();        //rss
            //channels = root.getChildNodes();       //channels
             
             
             NodeList list = doc.getElementsByTagName("yweather:forecast");
             int size = list.getLength();
             for(int i = 0; i<(list.getLength());i++) {
                 //遍历每一个元素
                 Element elt = (Element) list.item(i);
                 String date = elt.getAttribute("date");
                 String day = elt.getAttribute("day");
                 String text = elt.getAttribute("text");
                 String low = elt.getAttribute("low");
                 String high = elt.getAttribute("high");
                                 
                }
             
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            Log.e("liyongjun", "网络连接超时!");
        } catch (ParserConfigurationException e) {
            e.printStackTrace();
        } catch (SAXException e) {
            e.printStackTrace();
        }
        
    }

获得城市代码(yahoo提供)

public String getCityCode(String name) throws UnsupportedEncodingException{
        String code = null;
        String paramString="http://query.yahooapis.com/v1/public/yql?q=select*from%20geo.places%20where%20text=%27"+name+"%27";
    try {    
         domfac = DocumentBuilderFactory
                 .newInstance();
         dombuilder = domfac.newDocumentBuilder();
         NodeList results;
         
         URL url = new URL(paramString);
         HttpURLConnection  urlConnection = (HttpURLConnection) url.openConnection();//打开一个连接  
         doc = dombuilder.parse(urlConnection.getInputStream());
         root = doc.getDocumentElement();
         results = root.getChildNodes();   //results

         if(results.getLength() > 0){
             Node node = results.item(0).getFirstChild(); //得到 item 第一个 place
             NodeList places = node.getChildNodes();        //得到place下所有子节点
             code = places.item(0).getFirstChild().getNodeValue();  //指向第一个place,拿到第一个子节点 wodid 获得value
         }
             
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (SAXException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ParserConfigurationException e) {
            e.printStackTrace();
        }

        return code;
    }

查询城市ID API

http://query.yahooapis.com/v1/public/yql?q=select*from geo.places where text= ' "+name+" ' ";
name:即需要的查询城市的英文名。



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值