java调用webservice天气预报(SOAP请求的方式获取天气信息并解析返回的XML)

Java调用webservice天气预报(SOAP请求的方式获取天气信息并解析返回的XML)

webservice网站为http://www.webxml.com.cn/WebServices/WeatherWS.asmx

[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. package webservice;  
  2.   
  3. import java.io.InputStream;    
  4. import java.io.OutputStream;    
  5. import java.io.OutputStreamWriter;    
  6. import java.net.URL;    
  7. import java.net.URLConnection;    
  8. import javax.xml.parsers.DocumentBuilder;    
  9. import javax.xml.parsers.DocumentBuilderFactory;    
  10. import org.w3c.dom.Document;    
  11. import org.w3c.dom.Node;    
  12. import org.w3c.dom.NodeList;    
  13.     
  14. /**  
  15.  * @ClassName WeatherUtils  
  16.  * @Description TODO 天气信息数据来源(http://www.webxml.com.cn/)  
  17.  * 根据城市或地区名称查询获得未来三天内天气情况、现在的天气实况、天气和生活指数:  
  18.  * 调用方法如下:输入参数:theCityName = 城市中文名称(国外城市可用英文)或城市代码(不输入默认为上海市),如:上海 或 58367,如有  
  19.  * 城市名称重复请使用城市代码查询(可通过 getSupportCity 或 getSupportDataSet 获得);返回数据: 一个一维数组 String(22),共有  
  20.  * 23个元素。String(0) 到 String(4):省份,城市,城市代码,城市图片名称,最后更新时间。String(5) 到 String(11):当天的 气温,  
  21.  * 概况,风向和风力,天气趋势开始图片名称(以下称:图标一),天气趋势结束图片名称(以下称:图标二),现在的天气实况,天气和生活  
  22.  * 指数。String(12) 到String(16):第二天的 气温,概况,风向和风力,图标一,图标二。String(17) 到 String(21):第三天的 气温,  
  23.  * 概况,风向和风力,图标一,图标二。String(22) 被查询的城市或地区的介绍  
  24.  * @author zyj jayzh1993@gmail.com  
  25.  * @date 2013-10-14  
  26.  */    
  27. public class WeatherUtil{    
  28.     /**  
  29.      * 获取SOAP的请求头,并替换其中的标志符号为用户输入的城市  
  30.      *   
  31.      * @param city  
  32.      *            用户输入的城市名称  
  33.      * @return 客户将要发送给服务器的SOAP请求  
  34.      */    
  35.     private static String getSoapRequest(String city) {    
  36.         StringBuilder sb = new StringBuilder();    
  37.         sb.append("<?xml version=\"1.0\" encoding=\"utf-8\"?>"    
  38.                     + "<soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" "    
  39.                     + "xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" "    
  40.                     + "xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">"    
  41.                     + "<soap:Body>    <getWeather xmlns=\"http://WebXml.com.cn/\">"    
  42.                     + "<theCityCode>" + city    
  43.                     + "</theCityCode>    </getWeather>"    
  44.                     + "</soap:Body></soap:Envelope>");    
  45.         return sb.toString();    
  46.     }    
  47.     
  48.     /**  
  49.      * 用户把SOAP请求发送给服务器端,并返回服务器点返回的输入流  
  50.      *   
  51.      * @param city  
  52.      *            用户输入的城市名称  
  53.      * @return 服务器端返回的输入流,供客户端读取  
  54.      * @throws Exception  
  55.      */    
  56.     private static InputStream getSoapInputStream(String city) throws Exception {    
  57.         try {    
  58.             String soap = getSoapRequest(city);    
  59.             if (soap == null) {    
  60.                 return null;    
  61.             }    
  62.             URL url = new URL(    
  63.                     "http://www.webxml.com.cn/WebServices/WeatherWS.asmx");    
  64.             URLConnection conn = url.openConnection();    
  65.             conn.setUseCaches(false);    
  66.             conn.setDoInput(true);    
  67.             conn.setDoOutput(true);    
  68.     
  69.             conn.setRequestProperty("Content-Length", Integer.toString(soap    
  70.                     .length()));    
  71.             conn.setRequestProperty("Content-Type""text/xml; charset=utf-8");    
  72.             conn.setRequestProperty("SOAPAction",    
  73.                     "http://WebXml.com.cn/getWeather");    
  74.     
  75.             OutputStream os = conn.getOutputStream();    
  76.             OutputStreamWriter osw = new OutputStreamWriter(os, "utf-8");    
  77.             osw.write(soap);    
  78.             osw.flush();    
  79.             osw.close();    
  80.     
  81.             InputStream is = conn.getInputStream();    
  82.             return is;    
  83.         } catch (Exception e) {    
  84.             e.printStackTrace();    
  85.             return null;    
  86.         }    
  87.     }    
  88.     
  89.     /**  
  90.      * 对服务器端返回的XML进行解析  
  91.      *   
  92.      * @param city  
  93.      *            用户输入的城市名称  
  94.      * @return 字符串 用#分割  
  95.      */    
  96.     public static String getWeather(String city) {    
  97.         try {    
  98.             Document doc;    
  99.             DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();    
  100.             dbf.setNamespaceAware(true);    
  101.             DocumentBuilder db = dbf.newDocumentBuilder();    
  102.             InputStream is = getSoapInputStream(city);    
  103.             doc = db.parse(is);    
  104.             NodeList nl = doc.getElementsByTagName("string");    
  105.             StringBuffer sb = new StringBuffer();    
  106.             for (int count = 0; count < nl.getLength(); count++) {    
  107.                 Node n = nl.item(count);    
  108.                 if(n.getFirstChild().getNodeValue().equals("查询结果为空!")) {    
  109.                     sb = new StringBuffer("#") ;    
  110.                     break ;    
  111.                 }    
  112.                 sb.append(n.getFirstChild().getNodeValue() + "#");    
  113.             }    
  114.             is.close();    
  115.             return sb.toString();    
  116.         } catch (Exception e) {    
  117.             e.printStackTrace();    
  118.             return null;    
  119.         }    
  120.     }    
  121.     /**  
  122.      * 测试  
  123.      * @param args  
  124.      * @throws Exception  
  125.      */    
  126.     public static void main(String[] args) throws Exception {    
  127.         String weatherInfo = getWeather("广州");    
  128.         System.out.println(weatherInfo);    
  129.     }    
  130. }    
结果:

  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值