免费的天气预报webservice接口

本文分享了几个免费的天气预报Webservice接口,包括如何使用这些接口获取城市天气信息的示例,如通过Java程序调用接口。还提到了Markdown编辑器的新功能,如图片拖拽、代码高亮、甘特图等。
摘要由CSDN通过智能技术生成

分享一下我老师大神的人工智能教程!零基础,通俗易懂!http://blog.csdn.net/jiangjunshow

也欢迎大家转载本篇文章。分享知识,造福人民,实现我们中华民族伟大复兴!

               

1. http://www.ayandy.com/Service.asmx?wsdl

 官网:http://www.ayandy.com

 

2. http://www.webservicex.net/globalweather.asmx?wsdl(英文的)

example:http://www.webservicex.net/globalweather.asmx/GetWeather?CityName=dalian&CountryName=china

 

3. http://webservice.webxml.com.cn/WebServices/WeatherWS.asmx?wsdl

网站:http://www.webxml.com.cn/zh_cn/index.aspx ,此网站提供各种webservice接口;

java程序:

   public class WeatherUtil {  
    private static String SERVICES_HOST = "www.webxml.com.cn";  
    private static String WEATHER_SERVICES_URL = "http://webservice.webxml.com.cn/WebServices/WeatherWS.asmx/";  
    private static String PROVINCE_CODE_URL = WEATHER_SERVICES_URL   
                                              + "getRegionProvince";  
    private static String CITY_CODE_URL = WEATHER_SERVICES_URL  
                                          + "getSupportCityString?theRegionCode=";  
    private static String WEATHER_QUERY_URL = WEATHER_SERVICES_URL  
                                              + "getWeather?theUserID=&theCityCode=";  
      
      
    private WeatherUtil(){}  
      
    public static void main(String[] args) throws Exception{      
        int provinceCode = getProvinceCode("辽宁");    //3119  
        int cityCode = getCityCode(provinceCode, "大连");    //974  
        List<String> weatherList = getWeather(cityCode);  
        for(String weather:weatherList){  
           System.out.println(weather);  
        }  
    }  
      
    public static int getProvinceCode(String provinceName){   
        Document document;  
        DocumentBuilderFactory documentBF = DocumentBuilderFactory.newInstance();  
        documentBF.setNamespaceAware(true);  
        int provinceCode = 0;  
        try{  
            DocumentBuilder documentB = documentBF.newDocumentBuilder();  
            InputStream inputStream = getSoapInputStream(PROVINCE_CODE_URL);    //具体webService相关  
            document = documentB.parse(inputStream);  
            NodeList nodeList = document.getElementsByTagName("string");    //具体webService相关  
            int len = nodeList.getLength();  
            for(int i = 0; i < len; i++){  
                Node n = nodeList.item(i);  
                String result = n.getFirstChild().getNodeValue();  
                String[] address = result.split(",");  
                String pName = address[0];  
                String pCode = address[1];  
                if(pName.equalsIgnoreCase(provinceName)){  
                    provinceCode = Integer.parseInt(pCode);  
                }   
            }  
            inputStream.close();  
        }catch(DOMException e){  
            e.printStackTrace();  
        }catch(ParserConfigurationException e){  
            e.printStackTrace();  
        }catch (SAXException e){      
            e.printStackTrace();  
        }catch(IOException e){  
            e.printStackTrace();  
        }  
        return provinceCode;  
    }  
      
    public static int getCityCode(int provinceCode, String cityName){     
        Document doc;  
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();  
        dbf.setNamespaceAware(true);  
        int cityCode = 0;  
        try{  
            DocumentBuilder db = dbf.newDocumentBuilder();  
            InputStream is = getSoapInputStream(CITY_CODE_URL + provinceCode);    //具体webService相关  
            doc = db.parse(is);  
            NodeList nl = doc.getElementsByTagName("string");    //具体webService相关  
            int len = nl.getLength();  
            for(int i = 0; i < len; i++){  
                Node n = nl.item(i);  
                String result = n.getFirstChild().getNodeValue();  
                String[] address = result.split(",");  
                String cName = address[0];  
                String cCode = address[1];  
                if(cName.equalsIgnoreCase(cityName)){  
                    cityCode = Integer.parseInt(cCode);  
                }  
            }  
            is.close();  
        }catch(DOMException e){  
            e.printStackTrace();  
        }catch(ParserConfigurationException e){  
            e.printStackTrace();  
        }catch (SAXException e){  
            e.printStackTrace();  
        }catch(IOException e) {  
            e.printStackTrace();  
        }  
        return cityCode;  
    }  
      
    public static InputStream getSoapInputStream(String url){  
        InputStream inputStream = null;  
      

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值