webservice引用天气预报实例
来源:互联网 发布:知乎取消赞同 编辑:程序博客网 时间:2024/04/29 22:10
import java.io.IOException; import java.io.InputStream; import java.io.UnsupportedEncodingException; import java.net.MalformedURLException; import java.net.URL; import java.net.URLConnection; import java.util.ArrayList; import java.util.List; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.ParserConfigurationException; import org.w3c.dom.DOMException; import org.w3c.dom.Document; import org.w3c.dom.Node; import org.w3c.dom.NodeList; import org.xml.sax.SAXException; 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; } }
0 0
- webservice引用天气预报实例
- java调用WebService天气预报实例
- java调用WebService天气预报实例
- 一个webservice获取天气预报的实例
- webservice通信调用天气预报接口实例
- webservice通信调用天气预报接口实例
- webservice通信调用天气预报接口实例
- [WEBSERVICE]天气预报
- 天气预报webservice
- webservice天气预报
- 引用 天气预报
- 使用ksoap2 调用 WebService(实例:调用天气预报服务)
- Flex构建WebService应用实例 (查询城市天气预报)
- 使用ksoap2 调用 WebService(实例:调用天气预报服务)
- 使用ksoap2 调用 WebService(实例:调用天气预报服务)
- 天气预报webservice代码
- 天气预报webservice代码
- 天气预报 WebService 简单例子
- java.lang.IllegalArgumentException: Can't convert argument: null
- 百度O2O两条腿:百度地图和百度糯米
- 关于QT出现无法解析的外部符号
- FZEasyFile——简单易用的iOS沙盒文件操作组件
- Ajax工作原理
- webservice引用天气预报实例
- Git版本管理初探
- Cocos2D-X2.2.3学习笔记1(vs开发环境和Android环境交叉编译)
- 极建立跨集团核电厂核事故应急救援
- java读写和修改ini文件汇总
- 单选框点击文字也能选中
- Installation error: INSTALL_FAILED_CONFLICTING_PROVIDER
- 关于内存的一些理解
- iOS开发教程:Storyboard全解析-第二部分