webservice 天气预报的接口
2018-01-05 21:05
288 查看
下面是在学习webService时整理的一些琐碎:
显示结果:
package cn.com.snxun.util; import java.io.IOException; import java.io.InputStream; import java.io.UnsupportedEncodingException; import java.net.URL; import java.net.URLConnection; import java.util.ArrayList; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.ParserConfigurationException; import org.w3c.dom.Document; import org.w3c.dom.Element; import org.w3c.dom.Node; import org.w3c.dom.NodeList; import org.xml.sax.SAXException; public final class WeatherUtils { private static String SERVICES_HOST = "www.webxml.com.cn"; private static String WEATHER_SERVICES_URL = "http://www.webxml.com.cn/WebServices/WeatherWebService.asmx/"; // 获得本天气预报Web Services支持的洲、国内外省份和城市信息 private static String SUPPORT_CITY_URL = WEATHER_SERVICES_URL + "getSupportCity?byProvinceName="; // 根据城市或地区名称查询 private static String WEATHER_QUERY_URL = WEATHER_SERVICES_URL + "getWeatherbyCityName?theCityName="; private WeatherUtils() { } /** * 将文件转换为流 * * @param url * @return * @throws IOException */ public static InputStream getSoapInputStream(String url) throws IOException { InputStream is = null; URL U = new URL(url); URLConnection conn = U.openConnection(); conn.setRequestProperty("Host", SERVICES_HOST); conn.connect(); is = conn.getInputStream(); return is; } // 取得支持的城市列表 public static ArrayList<String> getSupportCity(String provinceName) throws ParserConfigurationException, IOException, SAXException { ArrayList cityList = null; Document doc; // 首先得到:得到 DOM 解析器的工厂实例 DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); // 如果生成的解析器将提供对 xml 名称空间的支持,则为 true;否则为 false dbf.setNamespaceAware(true); // 然后从 DOM 工厂获得 DOM 解析器 DocumentBuilder db = dbf.newDocumentBuilder(); InputStream is = getSoapInputStream(SUPPORT_CITY_URL + provinceName); doc = db.parse(is); NodeList nl = doc.getElementsByTagName("string"); int len = nl.getLength(); cityList = new ArrayList<String>(len); for (int i = 0; i < len; i++) { Node n = nl.item(i); String city = n.getFirstChild().getNodeValue(); cityList.add(city); } is.close(); return cityList; } // 取得城市的天气 public static ArrayList<String> getWeather(String city) throws ParserConfigurationException, UnsupportedEncodingException, IOException, SAXException { ArrayList weatherList = null; // 首先得到:得到 DOM 解析器的工厂实例 DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); // 如果生成的解析器将提供对 xml 名称空间的支持,则为 true;否则为 false dbf.setNamespaceAware(true); // 然后从 DOM 工厂获得 DOM 解析器 DocumentBuilder db = dbf.newDocumentBuilder(); // 将XML文件转成流 InputStream is = getSoapInputStream(WEATHER_QUERY_URL + city); // 解析 XML 文档的输入流,得到一个 Document Document doc = db.parse(is); // 得到 XML 文档的根节点 Element root = doc.getDocumentElement(); // 得到节点的子节点 NodeList nl = root.getElementsByTagName("string"); int len = nl.getLength(); weatherList = new ArrayList<String>(len); for (int i = 0; i < len; i++) { Node n = nl.item(i); String weather = n.getFirstChild().getNodeValue(); weatherList.add(weather); } is.close(); return weatherList; } public static void main(String[] args) throws Exception { ArrayList<String> weatherList = WeatherUtils.getWeather("上海"); //ArrayList<String> weatherList = WeatherUtils.getSupportCity("江苏"); for (String weather : weatherList) { System.out.println(weather); } } }
显示结果:
直辖市 上海 58367 58367.jpg 2018/1/5 21:02:46 3℃/7℃ 1月5日 阴转多云 北风转东北风小于3级 2.gif 1.gif 今日天气实况:气温:3℃;风向/风力:静风 0级;湿度:70%;紫外线强度:最弱。空气质量:较差。 紫外线指数:最弱,辐射弱,涂擦SPF8-12防晒护肤品。 健臻·血糖指数:较易波动,血糖较易波动,注意监测。 感冒指数:较易发,天较凉,增加衣服,注意防护。 穿衣指数:冷,建议着棉衣加羊毛衫等冬季服装。 洗车指数:较适宜,无雨且风力较小,易保持清洁度。 空气污染指数:较差,气象条件较不利于空气污染物扩散。。 4℃/8℃ 1月6日 阴转中雨 东风小于3级 2.gif 8.gif 4℃/11℃ 1月7日 中雨转小雨 东南风小于3级转西北风4-5级 8.gif 7.gif 上海简称:沪,位置:上海地处长江三角洲前缘,东濒东海,南临杭州湾,西接江苏,浙江两省,北界长江入海,正当我国南北岸线的中部,北纬31°14′,东经121°29′。面积:总面积7823.5平方公里。人口:人口1000多万。上海丰富的人文资源、迷人的城市风貌、繁华的商业街市和欢乐的节庆活动形成了独特的都市景观。游览上海,不仅能体验到大都市中西合壁、商儒交融、八方来风的氛围,而且能感受到这个城市人流熙攘、车水马龙、灯火璀璨的活力。上海在中国现代史上占有着十分重要的地位,她是中国共产党的诞生地。许多震动中外的历史事件在这里发生,留下了众多的革命遗迹,处处为您讲述着一个个使人永不忘怀的可歌可泣的故事,成为包含民俗的人文景观和纪念地。在上海,每到秋祭,纷至沓来的人们在这里祭祀先烈、缅怀革命历史,已成为了一种风俗。大上海在中国近代历史中,曾是风起云涌可歌可泣的地方。在这里荟萃多少风云人物,散落在上海各处的不同住宅建筑,由于其主人的非同寻常,蕴含了耐人寻味的历史意义。这里曾留下许多革命先烈的足迹。瞻仰孙中山、宋庆龄、鲁迅等故居,会使您产生抚今追昔的深沉遐思,这里还有无数个达官贵人的住宅,探访一下李鸿章、蒋介石等人的公馆,可以联想起主人那段显赫的发迹史。
相关文章推荐
- WebService 天气预报webservice接口
- 查询天气预报的Webservice接口程序源代码
- Delphi 编写调用WebService接口的小程序(天气预报)
- 查询天气预报的Webservice接口程序源代码
- 免费的天气预报webservice接口
- 免费的天气预报webservice接口
- 免费的天气预报webservice接口 .保留
- Java调用WEBSERVICE接口实现天气预报和获得国内手机号码归属地省份、地区和手机卡类型信息
- 免费的天气预报webservice接口 .保留
- 免费的天气预报webservice接口
- 查询天气预报的Webservice接口程序源代码
- 免费的天气预报webservice接口
- 免费的webservice接口(天气预报/IP查询/股票查询/手机归属地等)
- 免费的天气预报webservice接口 .保留
- 免费的天气预报webservice接口
- 免费的天气预报webservice接口
- 查询天气预报的Webservice接口程序源代码
- bos webservice接口后登陆用户会覆盖前用户的context的解决方法
- 【挖坑】webservice接口、http接口的监控
- 【webservice】调用webservice接口