主页 > 学习笔记 > Java获取天气的API实现

Java获取天气的API实现

通过新浪提供的API实现获取当前的天气信息,代码如下:

import java.net.URL;

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 GetWeather {  
    static String[] city={"北京","天津","上海","重庆","石家庄","太原","沈阳","长春","哈尔滨","南京","杭州","合肥","福州","南昌","济南","郑州","武汉","长沙","广州","海口","成都","贵阳","昆明","西安","兰州","西宁","拉萨","南宁","呼和浩特","银川","乌鲁木齐","香港","台北","澳门"};  //各个城市
    static int[] day={0,1,2,3,4};   //哪一天的天气
    static String weather;  //保存天气情况
    static String high;  //保存当天最高温度
    static String low;  //保存当天最低温度
    public void getweather()   //获取天气函数
    {  
        URL ur;  
             
        try {  
           
         DocumentBuilderFactory domfac=DocumentBuilderFactory.newInstance();  //相关这个类的使用,可以去网上搜索,下同,不做详细介绍
         DocumentBuilder dombuilder=domfac.newDocumentBuilder();
         Document doc;
         Element root;
         NodeList books;
         for (String str : city) {     //循环访问获取各个地区不同天气情况
          ur = new URL("http://php.weather.sina.com.cn/xml.php?city="+str+"&password=DJOYnieT8234jlsK&day="+day);
          doc=dombuilder.parse(ur.openStream());
                root=doc.getDocumentElement();
                books=root.getChildNodes();
                for(Node node=books.item(1).getFirstChild();node!=null;node=node.getNextSibling()){
                 if(node.getNodeType()==Node.ELEMENT_NODE){
                  if(node.getNodeName().equals("status1"))weather=node.getTextContent();  //获取到天气情况
                  else if(node.getNodeName().equals("temperature1"))high=node.getTextContent();  //获取到最高温度
                  else if(node.getNodeName().equals("temperature2"))low=node.getTextContent();   //获取到最低温度
                 }
                }
                System.out.println(str+" "+weather+" "+low+"℃~"+high+"℃");  //前台输出
   }
           
                 }catch(Exception e){System.out.println("获取天气失败:"+e);}
    }  
    public static void main(String [] arg)  
    {  
              new GetWeather().getweather();  //主接口函数调用执行方法
    }  
 
}

评论:1

  1. tianyu 回复
    2013 年 4 月 11 日 于 下午 8:02

    附录:新浪支持的中国城市列表

    “阿巴嘎旗”,”阿城”,”安达”,”安康”,”安宁”,”安庆”,”鞍山”,”安顺”,”安阳”,”安县”,”百色”,”巴仑台”,”保定”,”宝鸡”,”宝清”,”保山”,”包头”,”巴中”,”巴塘”,”巴音毛道”,”巴音布鲁克”,”北塔山”,”北海”,”北京”,”蚌埠”,”本溪”,”毕节”,”博克图”,”长岛”,”长白”,”长春”,”常德”,”昌吉”,”长岭”,”长沙”,”常熟”,”长汀”,”常州”,”朝阳”,”承德”,”成都”,”郴州”,”赤峰”,”重庆”,”楚雄”,”达拉特旗”,”大理”,”大连”,”丹东”,”单县”,”稻城”,”大同”,”大通”,”大悟”,”大兴”,”德格”,”德令哈”,”丁青”,”定海”,”定陶”,”定西”,”东方”,”东台”,”都兰”,”敦化”,”敦煌”,”多伦”,”独山”,”伊金霍洛旗”,”峨眉山”,”恩施”,”二连浩特”,”房县”,”凤城”,”奉节”,”丰宁”,”佛冈”,”佛山”,”扶绥”,”福鼎”,”富锦”,”抚顺”,”阜阳”,”富蕴”,”福州”,”刚察”,”甘谷”,”赣榆”,”赣州”,”高要”,”高邑”,”甘孜”,”耿马”,”格尔木”,”珙县”,”拐子湖”,”广安”,”广昌”,”广南”,”广州”,”桂林”,”桂平”,”贵阳”,”固始”,”固阳”,”海城”,”海口”,”海拉尔”,”海林”,”海伦”,”海宁”,”海晏”,”汉沽”,”杭州”,”汉中”,”哈尔滨”,”河池”,”合川”,”合肥”,”合江”,”河南”,”河曲”,”河源”,”菏泽”,”呼和浩特”,”香港”,”和田”,”化德”,”桦甸”,”怀来”,”华家岭”,”黄山”,”黄石”,”会理”,”惠民”,”会泽”,”惠州”,”呼兰”,”虎林”,”呼玛”,”霍山”,”湖州”,”吉兰太”,”扎鲁特旗”,”集安”,”江城”,”江津”,”江陵”,”江门”,”建阳”,”胶南”,”嘉兴”,”介休”,”吉林”,”济南”,”景德镇”,”静海”,”精河”,”景洪”,”靖远”,”济宁”,”锦州”,”九江”,”九龙”,”酒泉”,”九台”,”鸡西”,”朱日和”,”句容”,”哈巴河”,”开封”,”开阳”,”康定”,”克拉玛依”,”喀什”,”克山”,”库尔勒”,”库伦旗”,”宽甸”,”昆明”,”库车”,”澜沧”,”阆中”,”兰西”,”兰州”,”乐亭”,”拉萨”,”乐至”,”梁平”,”连平”,”辽阳”,”利津”,”临沧”,”零陵”,”陵县”,”临海”,”临河”,”临江”,”临潼”,”林西”,”灵石”,”丽水”,”理塘”,”浏阳”,”柳州”,”溧阳”,”龙口”,”龙州”,”庐山”,”漯河”,”罗甸”,”洛阳”,”卢氏”,”泸溪”,”泸州”,”澳门”,”麻城”,”玛多”,”玛纳斯”,”满都拉”,”茂名”,”梅县”,”眉山”,”勐腊”,”蒙山”,”孟州”,”蒙自”,”绵阳”,”民和”,”民勤”,”闽清”,”米泉”,”密云”,”漠河”,”牡丹江”,”那曲”,”南昌”,”南城”,”南充”,”南京”,”南宁”,”南平”,”南通”,”南县”,”南阳”,”南岳”,”那坡”,”那仁宝力格”,”内江”,”嫩江”,”鄂托克旗”,”平凉”,”平潭”,”平武”,”平遥”,”平阴”,”皮山”,”泊头”,”蒲城”,”恰卜恰”,”青岛”,”青龙”,”清远”,”钦州”,”琼海”,”齐齐哈尔”,”奇台”,”泉州”,”曲麻莱”,”日照”,”榕江”,”瑞丽”,”若羌”,”桑植”,”三水”,”上海”,”尚志”,”汕头”,”汕尾”,”韶关”,”邵武”,”绍兴”,”邵阳”,”嵊泗”,”沈阳”,”深圳”,”射阳”,”石家庄”,”石楼”,”石浦”,”狮泉河”,”双城”,”沭阳”,”思茅”,”思南”,”四平”,”松潘”,”绥芬河”,”绥宁”,”孙吴”,”塔城”,”塔河”,”泰来”,”太原”,”塘沽”,”唐山”,”腾冲”,”天津”,”天水”,”铁岭”,”铁干里克”,”铁力”,”通道”,”通河”,”通辽”,”图里河”,”吐鲁番”,”乌鲁木齐”,”乌苏”,”万源”,”围场”,”潍坊”,”渭南”,”威宁”,”温州”,”伍道梁”,”武都”,”武冈”,”武汉”,”芜湖”,”武鸣”,”五台山”,”武夷山”,”梧州”,”申扎”,”西乌珠穆沁旗”,”西安”,”厦门”,”湘潭”,”咸阳”,”孝感”,”西昌”,”西丰”,”西华”,”锡林浩特”,”新巴尔虎右旗”,”新安”,”兴仁”,”邢台”,”西宁”,”新乡”,”信阳”,”信宜”,”寻乌”,”徐闻”,”徐州”,”雅安”,”延安”,”盐池”,”盐城”,”阳江”,”阳泉”,”延吉”,”兖州”,”叶县”,”宜宾”,”宜昌”,”伊春”,”银川”,”营口”,”伊宁”,”义乌”,”益阳”,”沂源”,”永安”,”永川”,”永宁”,”酉阳”,”攸县”,”元江”,”沅陵”,”元谋”,”原平”,”虞城”,”榆次”,”岳阳”,”榆林”,”玉门镇”,”榆社”,”榆树”,”杂多”,”枣阳”,”漳平”,”张家口”,”章丘”,”彰武”,”张掖”,”湛江”,”沾益”,”昭通”,”正定”,”郑州”,”镇江”,”枝江”,”中宁”,”钟祥”,”周村”,”驻马店”,”涿州”,”淄博”,”遵义”

发表评论

电子邮件地址不会被公开。 必填项已用*标注