微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

免费的天气预报webservice接口

免费的天气预报webservice接口  

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;   
        try{   
            URL urlObj = new URL(url);   
            URLConnection urlConn = urlObj.openConnection();   
            urlConn.setRequestProperty("Host",SERVICES_HOST);    //具体webService相关   
            urlConn.connect();   
            inputStream = urlConn.getInputStream();   
        }catch(MalformedURLException e){   
            e.printstacktrace();   
        }catch(IOException e){   
            e.printstacktrace();   
        }   
        return inputStream;   
    }   
       
    public static List<String> getWeather(int cityCode){   
        List<String> weatherList = new ArrayList<String>();           Document document;   
        DocumentBuilderFactory documentBF = DocumentBuilderFactory.newInstance();   
        documentBF.setNamespaceAware(true);   
        try{   
            DocumentBuilder documentB = documentBF.newDocumentBuilder();   
            InputStream inputStream = getSoapInputStream(WEATHER_QUERY_URL + cityCode);   
            document = documentB.parse(inputStream);   
            NodeList nl = document.getElementsByTagName("string");   
            int len = nl.getLength();   
            for(int i = 0; i < len; i++){   
             Node n = nl.item(i);   
                String weather = n.getFirstChild().getNodeValue();   
                weatherList.add(weather);   
            }   
            inputStream.close();   
        }catch(UnsupportedEncodingException e){   
            e.printstacktrace();   
        }catch (DOMException e){   
            e.printstacktrace();   
        }catch (ParserConfigurationException e){   
            e.printstacktrace();   
        }catch(SAXException e){   
            e.printstacktrace();   
        } catch (IOException e){   
            e.printstacktrace();   
        }   
        return weatherList;   
    }  

4. 完全免费的带各种插件的天气预报接口(嵌入页面代码和风格可任选【12种】):

 http://service.weather.com.cn/plugin/index.shtml

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐