WebServicel通过城市名查询天气
2017-12-14 15:19
127 查看
网站: WebXml
通过城市名称查天气
接口类:
实现接口类:
通过城市名称查天气
接口类:
public interface WeatherService { void getWeatherByCity(String cityName); }
实现接口类:
public class WeatherServiceImpl implements WeatherService { /** * 以字符串的形式,准备请求头 * * @param cityname 城市名 * @return */ public String getRequestheaderToString(String cityname) { String reqStr = "<?xml version=\"1.0\" encoding=\"utf-8\"?>" + "<soap12:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap12=\"http://www.w3.org/2003/05/soap-envelope\">" + " <soap12:Body>" + " <getWeatherbyCityName xmlns=\"http://WebXml.com.cn/\">" + " <theCityName>" + cityname + "</theCityName>" + " </getWeatherbyCityName>" + " </soap12:Body>" + "</soap12:Envelope>"; return reqStr; } /** * 通过HttpURLConnection来发送请求,并返回结果 * * @param requestString 请求头的文本信息 * @return 以流的形式返回查询结果(xml文档) */ public InputStream sendRequestSoap(String requestString) { try { URL url = new URL("http://ws.webxml.com.cn/WebServices/WeatherWebService.asmx"); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setDoInput(true); conn.setDoOutput(true); conn.setUseCaches(false); conn.setRequestProperty("Host", "ws.webxml.com.cn"); conn.setRequestProperty("Content-Type", "application/soap+xml;charset=utf-8"); conn.setRequestProperty("Content-Length", requestString.length() + ""); OutputStream os = conn.getOutputStream(); OutputStreamWriter writer = new OutputStreamWriter(os, "utf-8"); writer.write(requestString); writer.flush(); writer.close(); return conn.getInputStream(); } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return null; } public void getWeatherByCity(String cityName) { InputStream is = this.sendRequestSoap(this.getRequestheade 4000 rToString(cityName)); try { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = factory.newDocumentBuilder(); Document doc = builder.parse(is); NodeList list = doc.getElementsByTagName("string"); if (!list.item(0).getFirstChild().getNodeValue().equals("查询结果为空")) { for (int i = 0; i < list.getLength(); i++) { Node node = list.item(i); System.out.println(node.getFirstChild().getNodeValue()); } } else { System.out.println("查询结果为空!"); } } catch (ParserConfigurationException e) { e.printStackTrace(); } catch (SAXException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } /** * 测试 * * @param args */ public static void main(String[] args) { WeatherServiceImpl service = new WeatherServiceImpl(); service.getWeatherByCity("深圳"); } }
相关文章推荐
- PHP通过气象局开放API查询指定城市(区县)天气
- axis2通过城市名称调用.net写的asmx WebService查询天气实例 .
- axis2通过城市名称调用.net写的asmx WebService查询天气实例
- Web引用 查询手机归属地、城市天气
- Android使用"中国天气网"API数据通过城市名称获取天气情况
- 三天完成小应用 大开天气——通过省市县数据库来制作城市选定页面
- 如何通过web of science查询一个学者每一年的h-index?(h指数)
- python 查询天气(输入城市名,输出天气)
- Web项目实现通过经纬度获取城市信息---利用百度地图
- 天气预报公网查询城市的天气预报提示:查询结果为空!,这城市或区域暂时不被支持。
- Android使用"中国天气网"API数据通过城市名称获取天气情况
- 全球主要城市实时天气查询
- 根据城市查询天气
- jquery这个webservice根据选择城市查询天气
- PHP微信开发之查询城市天气
- 输入城市名查询未来5天的天气
- 通过经纬度获取城市yahoo天气预报(最新2013-4-17)
- 解决web程序获取不到his接口数据(通过查询his SQL server 视图)
- 调用天气api实现查询各城市天气
- JSON方法查询某城市天气