axis WebServices 完美调用天气预报,查询、显示 代码!

axis WebServices 完美调用天气预报,查询、显示 代码!

效果:

jsp页面:

 

[html][/html] view plaincopy

  1. <%@ page language=”java” import=”java.util.*” pageEncoding=”utf-8″%>
  2. <%@ taglib prefix=”c” uri=”http://java.sun.com/jsp/jstl/core” %>
  3. <!DOCTYPE HTML PUBLIC “-//W3C//DTD HTML 4.01 Transitional//EN”>
  4. <html>
  5.     <head>
  6.         <title>城市天气信息</title>
  7.     </head>
  8. <style>
  9. body{
  10.     font-size:12px;
  11. }
  12. table{
  13.     border-collapse:sparate;
  14.     border-spacing:0px;
  15. }
  16. td{
  17.     padding:0px;
  18.     border:0px solid #000;
  19.     text-align:center;
  20.     font-size:12px;
  21.     color:#2A5CAA;
  22.     border-color:#2A5CAA;
  23. }
  24. .noMess{
  25.     text-align: center;
  26.     text-valign: center;
  27. }
  28. </style>
  29.     <body>
  30.     <form action=”webservice.action” method=”post” name=”form1″>
  31.         <input type=”text” name=”city” /> <input type=”submit” value=”查询”  id=”sub”/>
  32.     </form>
  33.     <br/>
  34.     <!– 显示天气 –>
  35.     <c:if test=”${not empty weathers}”>
  36.     <table border=”0″ cellpadding=”0″ cellspacing=”0″ width=”380″>
  37.        <tr>
  38.            <td>${weathers._regionFirst}  ${weathers._regionSecond} 72小时天气预报</td>
  39.            <td>最新上报时间:${weathers._reportTime}</td>
  40.        </tr>
  41.        <tr>
  42.            <td colspan=”2″>
  43.                <!– 今天天气 –>
  44.                <table border=”0″ cellpadding=”0″ cellspacing=”0″>
  45.                 <tr><td>
  46.                ${weathers._todayDetail}
  47.                </td></tr>
  48.                <tr>
  49.                    <td>${weathers._todayDate}  ${weathers._todayTemperature}</td>
  50.                </tr>
  51.                <tr>
  52.                    <td> ${weathers._todayWind}</td>
  53.                </tr>
  54.                <tr>
  55.                    <td>
  56.                        <img src=”./images/weather/${weathers._todayPic_1}”/>
  57.                        <img src=”./images/weather/${weathers._todayPic_2}”>
  58.                    </td>
  59.                </tr>
  60.                <tr>
  61.                    <td></td>
  62.                </tr>
  63.                </table>
  64.            </td>
  65.        </tr>
  66.        <tr>
  67.            <td colspan=”2″>
  68.               <!– 明天天气 –>
  69.                <table border=”0″ cellpadding=”0″ cellspacing=”0″>
  70.                <tr>
  71.                    <td>${weathers._tomorrowDate}  ${weathers._tomorrowTemperature}</td>
  72.                </tr>
  73.                <tr>
  74.                    <td>${weathers._tomorrowWind}</td>
  75.                </tr>
  76.                <tr>
  77.                    <td>
  78.                        <img src=”./images/weather/${weathers._tomorrowPic_1}”/>
  79.                        <img src=”./images/weather/${weathers._tomorrowPic_2}”>
  80.                    </td>
  81.                </tr>
  82.                <tr>
  83.                    <td></td>
  84.                </tr>
  85.                </table>
  86.            </td>
  87.        </tr>
  88.        <tr>
  89.            <td colspan=”2″>
  90.                <!– 后天天气 –>
  91.                <table border=”0″ cellpadding=”0″ cellspacing=”0″>
  92.                <tr>
  93.                    <td>${weathers._affterTomorrowDate}  ${weathers._affterTomorrowTemperature}</td>
  94.                </tr>
  95.                <tr>
  96.                    <td>${weathers._affterTomorrowWind}</td>
  97.                </tr>
  98.                <tr>
  99.                    <td>
  100.                        <img src=”./images/weather/${weathers._affterTomorrowPic_1}”/>
  101.                        <img src=”./images/weather/${weathers._affterTomorrowPic_2}”>
  102.                    </td>
  103.                </tr>
  104.                <tr>
  105.                    <td></td>
  106.                </tr>
  107.                </table>
  108.             </td>
  109.         </tr>
  110.     </table>
  111.     </c:if>
  112. <!– 自动加载     ajax 方式 等写
  113. <script type=”text/javascript”>
  114.   window.onload= function(){
  115.   //location.href=form1.submit();
  116.   document.getElementById(“sub”).click();
  117.   };
  118. </script>
  119. –>
  120.     </body>
  121. </html>

 

 

struts.xml:

 

[html][/html] view plaincopy

  1. <?xml version=”1.0″ encoding=”UTF-8″ ?>
  2. <!DOCTYPE struts PUBLIC
  3.     “-//Apache Software Foundation//DTD Struts Configuration 2.3//EN”
  4.     “http://struts.apache.org/dtds/struts-2.3.dtd”>
  5. <struts>
  6.     <package name=”default” namespace=”/” extends=”struts-default”>
  7.         <action name=”webservice” class=”accp.action.WebservicesAction”>
  8.             <result name=”success”>
  9.             index.jsp
  10.             </result>
  11.         </action>
  12.     </package>
  13. </struts>

 

 
action代码:

 

[html][/html] view plaincopy

  1. package accp.action;
  2. import java.util.Hashtable;
  3. import accp.util.WeatherUtil;
  4. import com.opensymphony.xwork2.ActionContext;
  5. import com.opensymphony.xwork2.ActionSupport;
  6. public class WebservicesAction extends ActionSupport {
  7.     private String city;
  8.     //存放天气信息
  9.     Hashtable<String,Object> weathers=new Hashtable<String,Object>();
  10.     public String getCity() {
  11.         return city;
  12.     }
  13.     public void setCity(String city) {
  14.         this.city = city;
  15.     }
  16.     public String execute() throws Exception {
  17.     System.out.println(“—–开始查询——-“);
  18.     if(“”.equals(city)||null==city){
  19.         city=”广州”;
  20.     }
  21.     //得到天气信息
  22.     weathers=WeatherUtil.getInstancce().getWea(city);
  23.     ActionContext.getContext().put(“weathers”, weathers);
  24.     return “success”;
  25.     }
  26. }

实现类代码:

 

 

[html][/html] view plaincopy

  1. package accp.util;
  2. import java.io.InputStream;
  3. import java.io.OutputStream;
  4. import java.io.OutputStreamWriter;
  5. import java.net.MalformedURLException;
  6. import java.net.URL;
  7. import java.net.URLConnection;
  8. import java.util.Hashtable;
  9. import javax.xml.parsers.DocumentBuilder;
  10. import javax.xml.parsers.DocumentBuilderFactory;
  11. import org.w3c.dom.Document;
  12. import org.w3c.dom.Node;
  13. import org.w3c.dom.NodeList;
  14. public class WeatherUtil {
  15.     // 编码
  16.         private String enCoding =”utf-8″;
  17.         // 目标url
  18.         private String targetWebserviceUri=”http://www.webxml.com.cn/WebServices/WeatherWebService.asmx”;
  19.         //private String targetWebserviceUri = “http://webservice.webxml.com.cn/WebServices/WeatherWS.asmx?wsdl”;
  20.     private WeatherUtil(){};
  21.     public static WeatherUtil getInstancce(){
  22.         return new WeatherUtil();
  23.     }
  24.      /**
  25.         * 对服务器端返回的XML进行解析
  26.         * WeatherObj 常量
  27.         * @param city 用户输入的城市名称
  28.         * @return 字符串 用,分割
  29.        * @throws Exception
  30.         */
  31.     public Hashtable<String,Object> getWea(String city) throws Exception{
  32.     Hashtable<String,Object> weathers=new Hashtable<String,Object>();
  33.     Document doc=null;
  34.     DocumentBuilderFactory  dbf=DocumentBuilderFactory.newInstance();
  35.     DocumentBuilder db=dbf.newDocumentBuilder();
  36.     InputStream is=getResponseSOAP(city);//获取流
  37.     doc=db.parse(is); //获取 xml文档
  38.     NodeList nl=doc.getElementsByTagName(“string”);//获取所有节点
  39.    //循环读取 并放入hashtable
  40.     System.out.println(“元素Length:”+nl.getLength());
  41.    for(int i =0;i<nl.getLength();i++){
  42.        Node n = nl.item(i);
  43.        String nodeValue=n.getFirstChild().getNodeValue();
  44.        if(i !=11){
  45.            System.out.println(nodeValue);
  46.            weathers.put(WeatherObj.input.get(i), nodeValue) ;
  47.        }
  48.    }
  49.    return weathers;
  50.     }
  51.     /**
  52.      * 封装xml
  53.      */
  54.     public String getRequestSOAP(String city){
  55.          StringBuffer sb = new StringBuffer();
  56.             sb.append(“<?xml version=\”1.0\” encoding=\”utf-8\”?>”);
  57.             sb.append(“<soap:Envelope xmlns:xsi=\”http://www.w3.org/2001/XMLSchema-instance\” xmlns:xsd=\”http://www.w3.org/2001/XMLSchema\” xmlns:soap=\”http://schemas.xmlsoap.org/soap/envelope/\”>”);
  58.             sb.append(“<soap:Body>”);
  59.             sb.append(“<getWeatherbyCityName xmlns=\”http://WebXml.com.cn/\”>”);
  60.             sb.append(” <theCityName>”+city+”</theCityName>”);
  61.             sb.append(“</getWeatherbyCityName>”);
  62.             sb.append(“</soap:Body>”);
  63.             sb.append(“</soap:Envelope>”);
  64.             return sb.toString();
  65.     }
  66.     /**
  67.      * @throws MalformedURLException
  68.      * 获得流
  69.      */
  70.  private InputStream getResponseSOAP(String city) throws Exception{
  71.     String requestSOAP=getRequestSOAP(city);
  72.        // 创建url
  73.        URL  url= new URL(targetWebserviceUri);
  74.        //得到url 的链接
  75.        URLConnection conn = url.openConnection();
  76.        conn.setUseCaches(false);
  77.        conn.setDoInput(true);
  78.        conn.setDoOutput(true);
  79.        // 设置请求的头信息
  80.        conn.setRequestProperty(“Content-Type”, “text/xml; charset=”+enCoding);
  81.        conn.setRequestProperty(“Content-Length”, requestSOAP.length()+””);
  82.        conn.setRequestProperty(“SOAPAction”, “http://WebXml.com.cn/getWeatherbyCityName”);
  83.      OutputStream os = conn.getOutputStream();
  84.      OutputStreamWriter osw = new OutputStreamWriter(os, enCoding);
  85.      osw.write(requestSOAP);
  86.      osw.flush();
  87.      osw.close();
  88.        return conn.getInputStream() ;
  89.     }
  90. }

hashtable 类代码

 

 

[html][/html] view plaincopy

  1. package accp.util;
  2. import java.util.Hashtable;
  3. /**
  4.  * @author hxb
  5.  *
  6.  */
  7. public class WeatherObj {
  8.     public static final String REGIONFIRST = “_regionFirst”;//上级行政区
  9.     public static final String REGIONSECOND = “_regionSecond”;//本级行政区
  10.     public static final String REGIONID = “_regionId”;//行政区id
  11.     public static final String REGIONPIC = “_regionPic”;//行政区图片
  12.     public static final String REPORTTIME = “_reportTime”;//最新上报时间
  13.     public static final String TODAYTEMPERATURE = “_todayTemperature”;//今天温度
  14.     public static final String TODAYDATE = “_todayDate”;//今天日期
  15.     public static final String TODAYWIND = “_todayWind”;//今天风况
  16.     public static final String TODAYPIC_1 = “_todayPic_1”;//今天天气图片1
  17.     public static final String TODAYPIC_2 = “_todayPic_2”;//今天天气图片2
  18.     public static final String TODAYDETAIL = “_todayDetail”;//今天天气实况
  19.     public static final String ZHISHU = “_zhiShu”;//各个指数
  20.     /*public static final String CHUANYIZHISHU = “穿衣指数:”;
  21.     public static final String GANMAOZHISHU = “感冒指数:”;
  22.     public static final String CHENLIANZHISHU = “晨练指数:”;
  23.     public static final String JIAOTONGZHISHU = “交通指数:”;
  24.     public static final String LIANGSHAIZHISHU = “晾晒指数:”;
  25.     public static final String LVYOUZHISHU = “旅游指数:”;
  26.     public static final String LUKUANGZHISHU = “路况指数:”;
  27.     public static final String SHUSHIDUZHISHU = “舒适度指数:”;   */
  28.  /*
  29.     public static final String CHUANYIZHISHU = “穿衣指数:”;
  30.     public static final String GUOMINZHISHU = “过敏指数:”;
  31.     public static final String YUANDONGZHISHU = “运动指数:”;
  32.     public static final String LIANGSHAIZHISHU = “晾晒指数:”;
  33.     public static final String LVYOUZHISHU = “旅游指数:”;
  34.     public static final String LUKUANGZHISHU = “路况指数:”;
  35.     public static final String SHUSHIDUZHISHU = “舒适度指数:”;
  36.     public static final String KONGQIWURANGZHISHU=”空气污染指数:”;
  37.     */
  38.     public static final String TOMORROWTEMPERATURE = “_tomorrowTemperature”;//明天温度
  39.     public static final String TOMORROWDATE = “_tomorrowDate”;//明天日期
  40.     public static final String TOMORROWWIND = “_tomorrowWind”;//明天风况
  41.     public static final String TOMORROWPIC_1 = “_tomorrowPic_1”;//明天天气图片1
  42.     public static final String TOMORROWPIC_2 = “_tomorrowPic_2”;//明天天气图片2
  43.     public static final String AFTERTOMORROWTEMPERATURE = “_affterTomorrowTemperature”;//后天温度
  44.     public static final String AFTERTOMORROWDATE = “_affterTomorrowDate”;//后天日期
  45.     public static final String AFTERTOMORROWWIND = “_affterTomorrowWind”;//后天风况
  46.     public static final String AFTERTOMORROWPIC_1 = “_affterTomorrowPic_1”;//后天天气图片1
  47.     public static final String AFTERTOMORROWPIC_2 = “_affterTomorrowPic_2”;//后天天气图片2
  48.     public static final String DESCRIPT = “_descript”;//本地介绍
  49.     public static final Hashtable<Integer,String> input = new Hashtable<Integer,String>(0);//常量与数量对应
  50.     static{
  51.         input.put(0, REGIONFIRST);
  52.         input.put(1, REGIONSECOND);
  53.         input.put(2, REGIONID);
  54.         input.put(3, REGIONPIC);
  55.         input.put(4, REPORTTIME);
  56.         input.put(5, TODAYTEMPERATURE);
  57.         input.put(6, TODAYDATE);
  58.         input.put(7, TODAYWIND);
  59.         input.put(8, TODAYPIC_1);
  60.         input.put(9, TODAYPIC_2);
  61.         input.put(10, TODAYDETAIL);
  62.         input.put(11, ZHISHU);
  63.         input.put(12, TOMORROWTEMPERATURE);
  64.         input.put(13, TOMORROWDATE);
  65.         input.put(14, TOMORROWWIND);
  66.         input.put(15, TOMORROWPIC_1);
  67.         input.put(16, TOMORROWPIC_2);
  68.         input.put(17, AFTERTOMORROWTEMPERATURE);
  69.         input.put(18, AFTERTOMORROWDATE);
  70.         input.put(19, AFTERTOMORROWWIND);
  71.         input.put(20, AFTERTOMORROWPIC_1);
  72.         input.put(21, AFTERTOMORROWPIC_2);
  73.         input.put(22, DESCRIPT);
  74.     }
  75.     private String name;
  76.     private String message;
  77.     public String getMessage() {
  78.         return message;
  79.     }
  80.     public void setMessage(String message) {
  81.         this.message = message;
  82.     }
  83.     public String getName() {
  84.         return name;
  85.     }
  86.     public void setName(String name) {
  87.         this.name = name;
  88.     }
  89. }

标签