axis WebServices 完美调用天气预报,查询、显示 代码!
axis WebServices 完美调用天气预报,查询、显示 代码!
效果:
jsp页面:
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head> <title>城市天气信息</title> </head>
<style>
body{
font-size:12px;
}
table{
border-collapse:sparate;
border-spacing:0px;
}
td{
padding:0px;
border:0px solid #000;
text-align:center;
font-size:12px;
color:#2A5CAA;
border-color:#2A5CAA;
}
.noMess{
text-align: center;
text-valign: center;
} </style>
<body> <form action="webservice.action" method="post" name="form1">
<input type="text" name="city" /> <input type="submit" value="查询" id="sub"/>
</form>
<br/> <!-- 显示天气 -->
<c:if test="${not empty weathers}">
<table border="0" cellpadding="0" cellspacing="0" width="380">
<tr>
<td>${weathers._regionFirst} ${weathers._regionSecond} 72小时天气预报</td>
<td>最新上报时间:${weathers._reportTime}</td>
</tr>
<tr>
<td colspan="2">
<!-- 今天天气 -->
<table border="0" cellpadding="0" cellspacing="0">
<tr><td>
${weathers._todayDetail}
</td></tr> <tr>
<td>${weathers._todayDate} ${weathers._todayTemperature}</td>
</tr> <tr>
<td> ${weathers._todayWind}</td>
</tr>
<tr>
<td>
<img src="./images/weather/${weathers._todayPic_1}"/>
<img src="./images/weather/${weathers._todayPic_2}">
</td>
</tr>
<tr>
<td></td>
</tr>
</table>
</td>
</tr>
<tr>
<td colspan="2">
<!-- 明天天气 -->
<table border="0" cellpadding="0" cellspacing="0">
<tr>
<td>${weathers._tomorrowDate} ${weathers._tomorrowTemperature}</td>
</tr>
<tr>
<td>${weathers._tomorrowWind}</td>
</tr>
<tr>
<td>
<img src="./images/weather/${weathers._tomorrowPic_1}"/>
<img src="./images/weather/${weathers._tomorrowPic_2}">
</td>
</tr>
<tr>
<td></td>
</tr>
</table>
</td>
</tr>
<tr>
<td colspan="2">
<!-- 后天天气 -->
<table border="0" cellpadding="0" cellspacing="0">
<tr>
<td>${weathers._affterTomorrowDate} ${weathers._affterTomorrowTemperature}</td>
</tr>
<tr>
<td>${weathers._affterTomorrowWind}</td>
</tr>
<tr>
<td>
<img src="./images/weather/${weathers._affterTomorrowPic_1}"/>
<img src="./images/weather/${weathers._affterTomorrowPic_2}">
</td>
</tr>
<tr>
<td></td>
</tr>
</table>
</td>
</tr>
</table>
</c:if>
<!-- 自动加载 ajax 方式 等写
<script type="text/javascript">
window.onload= function(){
//location.href=form1.submit();
document.getElementById("sub").click();
}; </script>
-->
</body>
</html>
struts.xml:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd"> <struts>
<package name="default" namespace="/" extends="struts-default"> <action name="webservice" class="accp.action.WebservicesAction">
<result name="success">
index.jsp
</result>
</action>
</package> </struts>
action代码:
package accp.action; import java.util.Hashtable; import accp.util.WeatherUtil; import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport; public class WebservicesAction extends ActionSupport {
private String city; //存放天气信息
Hashtable<String,Object> weathers=new Hashtable<String,Object>(); public String getCity() {
return city;
} public void setCity(String city) {
this.city = city;
} public String execute() throws Exception {
System.out.println("-----开始查询-------"); if("".equals(city)||null==city){
city="广州";
}
//得到天气信息 weathers=WeatherUtil.getInstancce().getWea(city);
ActionContext.getContext().put("weathers", weathers);
return "success";
} }
实现类代码:
package accp.util; import java.io.InputStream;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.Hashtable; import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList; public class WeatherUtil {
// 编码
private String enCoding ="utf-8";
// 目标url
private String targetWebserviceUri="http://www.webxml.com.cn/WebServices/WeatherWebService.asmx";
//private String targetWebserviceUri = "http://webservice.webxml.com.cn/WebServices/WeatherWS.asmx?wsdl";
private WeatherUtil(){}; public static WeatherUtil getInstancce(){
return new WeatherUtil();
} /**
* 对服务器端返回的XML进行解析
* WeatherObj 常量
* @param city 用户输入的城市名称
* @return 字符串 用,分割
* @throws Exception
*/
public Hashtable<String,Object> getWea(String city) throws Exception{ Hashtable<String,Object> weathers=new Hashtable<String,Object>();
Document doc=null; DocumentBuilderFactory dbf=DocumentBuilderFactory.newInstance();
DocumentBuilder db=dbf.newDocumentBuilder(); InputStream is=getResponseSOAP(city);//获取流 doc=db.parse(is); //获取 xml文档 NodeList nl=doc.getElementsByTagName("string");//获取所有节点
//循环读取 并放入hashtable
System.out.println("元素Length:"+nl.getLength()); for(int i =0;i<nl.getLength();i++){
Node n = nl.item(i);
String nodeValue=n.getFirstChild().getNodeValue(); if(i !=11){
System.out.println(nodeValue);
weathers.put(WeatherObj.input.get(i), nodeValue) ;
}
} return weathers; } /**
* 封装xml
*/
public String getRequestSOAP(String city){ StringBuffer sb = new StringBuffer();
sb.append("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
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/\">");
sb.append("<soap:Body>");
sb.append("<getWeatherbyCityName xmlns=\"http://WebXml.com.cn/\">");
sb.append(" <theCityName>"+city+"</theCityName>");
sb.append("</getWeatherbyCityName>");
sb.append("</soap:Body>");
sb.append("</soap:Envelope>"); return sb.toString(); } /**
* @throws MalformedURLException
* 获得流
*/
private InputStream getResponseSOAP(String city) throws Exception{
String requestSOAP=getRequestSOAP(city); // 创建url
URL url= new URL(targetWebserviceUri);
//得到url 的链接
URLConnection conn = url.openConnection();
conn.setUseCaches(false);
conn.setDoInput(true);
conn.setDoOutput(true);
// 设置请求的头信息
conn.setRequestProperty("Content-Type", "text/xml; charset="+enCoding);
conn.setRequestProperty("Content-Length", requestSOAP.length()+"");
conn.setRequestProperty("SOAPAction", "http://WebXml.com.cn/getWeatherbyCityName"); OutputStream os = conn.getOutputStream();
OutputStreamWriter osw = new OutputStreamWriter(os, enCoding);
osw.write(requestSOAP);
osw.flush();
osw.close(); return conn.getInputStream() ;
} }
hashtable 类代码
package accp.util;
import java.util.Hashtable; /**
* @author hxb
*
*/
public class WeatherObj { public static final String REGIONFIRST = "_regionFirst";//上级行政区
public static final String REGIONSECOND = "_regionSecond";//本级行政区
public static final String REGIONID = "_regionId";//行政区id
public static final String REGIONPIC = "_regionPic";//行政区图片
public static final String REPORTTIME = "_reportTime";//最新上报时间 public static final String TODAYTEMPERATURE = "_todayTemperature";//今天温度
public static final String TODAYDATE = "_todayDate";//今天日期
public static final String TODAYWIND = "_todayWind";//今天风况
public static final String TODAYPIC_1 = "_todayPic_1";//今天天气图片1
public static final String TODAYPIC_2 = "_todayPic_2";//今天天气图片2
public static final String TODAYDETAIL = "_todayDetail";//今天天气实况
public static final String ZHISHU = "_zhiShu";//各个指数 /*public static final String CHUANYIZHISHU = "穿衣指数:";
public static final String GANMAOZHISHU = "感冒指数:";
public static final String CHENLIANZHISHU = "晨练指数:";
public static final String JIAOTONGZHISHU = "交通指数:";
public static final String LIANGSHAIZHISHU = "晾晒指数:";
public static final String LVYOUZHISHU = "旅游指数:";
public static final String LUKUANGZHISHU = "路况指数:";
public static final String SHUSHIDUZHISHU = "舒适度指数:"; */
/*
public static final String CHUANYIZHISHU = "穿衣指数:";
public static final String GUOMINZHISHU = "过敏指数:";
public static final String YUANDONGZHISHU = "运动指数:";
public static final String LIANGSHAIZHISHU = "晾晒指数:";
public static final String LVYOUZHISHU = "旅游指数:";
public static final String LUKUANGZHISHU = "路况指数:";
public static final String SHUSHIDUZHISHU = "舒适度指数:";
public static final String KONGQIWURANGZHISHU="空气污染指数:";
*/
public static final String TOMORROWTEMPERATURE = "_tomorrowTemperature";//明天温度
public static final String TOMORROWDATE = "_tomorrowDate";//明天日期
public static final String TOMORROWWIND = "_tomorrowWind";//明天风况
public static final String TOMORROWPIC_1 = "_tomorrowPic_1";//明天天气图片1
public static final String TOMORROWPIC_2 = "_tomorrowPic_2";//明天天气图片2 public static final String AFTERTOMORROWTEMPERATURE = "_affterTomorrowTemperature";//后天温度
public static final String AFTERTOMORROWDATE = "_affterTomorrowDate";//后天日期
public static final String AFTERTOMORROWWIND = "_affterTomorrowWind";//后天风况
public static final String AFTERTOMORROWPIC_1 = "_affterTomorrowPic_1";//后天天气图片1
public static final String AFTERTOMORROWPIC_2 = "_affterTomorrowPic_2";//后天天气图片2
public static final String DESCRIPT = "_descript";//本地介绍 public static final Hashtable<Integer,String> input = new Hashtable<Integer,String>(0);//常量与数量对应 static{
input.put(0, REGIONFIRST);
input.put(1, REGIONSECOND);
input.put(2, REGIONID);
input.put(3, REGIONPIC);
input.put(4, REPORTTIME); input.put(5, TODAYTEMPERATURE);
input.put(6, TODAYDATE);
input.put(7, TODAYWIND);
input.put(8, TODAYPIC_1);
input.put(9, TODAYPIC_2);
input.put(10, TODAYDETAIL); input.put(11, ZHISHU); input.put(12, TOMORROWTEMPERATURE);
input.put(13, TOMORROWDATE);
input.put(14, TOMORROWWIND);
input.put(15, TOMORROWPIC_1);
input.put(16, TOMORROWPIC_2); input.put(17, AFTERTOMORROWTEMPERATURE);
input.put(18, AFTERTOMORROWDATE);
input.put(19, AFTERTOMORROWWIND);
input.put(20, AFTERTOMORROWPIC_1);
input.put(21, AFTERTOMORROWPIC_2); input.put(22, DESCRIPT);
} private String name; private String message; public String getMessage() {
return message;
} public void setMessage(String message) {
this.message = message;
} public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} }
axis WebServices 完美调用天气预报,查询、显示 代码!的更多相关文章
- 完美解决pycharm 不显示代码提示问题
pycharm 不显示代码提示 1.检查IDE省电模式是否关闭状态!!! file → power save mode 取消掉 2.检查代码提示是否成功开启. setting → Inspection ...
- PHP 最完美调用百度翻译接口代码示例 (原)
php调用百度翻译最新接口代码 问 题:写的过程遇到了一个问题,url拼接好的原翻译内容,appid,sign的地址直接输出到浏览器可以打开看到翻译后的返回值,但是各种curl,file_ ...
- C#调用天气预报网络服务
本程序通过调用网络上公开的天气预报网络服务来显示某个地区三天的天气,使用到的网络服务地址:http://www.webxml.com.cn/WebServices/WeatherWebService. ...
- 《ArcGIS Engine+C#实例开发教程》第八讲 属性数据表的查询显示
原文:<ArcGIS Engine+C#实例开发教程>第八讲 属性数据表的查询显示 第一讲 桌面GIS应用程序框架的建立 第二讲 菜单的添加及其实现 第三讲 MapControl与Page ...
- 根据判断PC浏览器类型和手机屏幕像素自动调用不同CSS的代码
1.媒体查询方法在 css 里面这样写 -------------------- @media screen and (min-width: 320px) and (max-width: 480px) ...
- Codecs是以plugin的形式被调用的(显示中文的codec plugin文件是qcncodecs4.dll),可静态载入和动态载入
作为非英语国家人员开发的类库,QT有充分的理由优先考虑支持Unicode和各国自定义字库编码.大家也知道了QT对软件Internationalization有一套完整的开发模型,包括专门为此写的lin ...
- Ajax实现在textbox中输入内容,动态从数据库中模糊查询显示到下拉框中
功能:在textbox中输入内容,动态从数据库模糊查询显示到下拉框中,以供选择 1.建立一aspx页面,html代码 <HTML> <HEAD> <title>We ...
- 程序处理数据库中值字段值为null的查询显示
1.如果你做了一个简单的注册界面,需要用户进行注册,但有些项是不必要填的,当用户完成注册时,数据库表中的相应字段的值会写入null,但如何将查询的字段的值null显示出来? 2.首先我们学习一下如何向 ...
- Delphi天气预报查询
Delphi天气预报查询 天气预报接口api(中国天气网) 开源免费天气预报接口API以及全国所有地区代码!!(国家气象局提供) 真正的中国天气api接口xml,json(求加精) ...
随机推荐
- sql server 系统表系统视图 及作用说明
sql server 系统视图,可分为: 目录视图 兼容性视图 动态管理视图和函数 信息架构视图 复制视图 系统表: sysaltfiles主数据库 保存数据库的文件 sysch ...
- 怎么设置tomcat管理员的用户名和密码
我们常常要进入Tomcat的管理界面来进行相应的操作,我们首先得有一个管理员的账户和密码.而Tomcat默认是没有管理员账户的,那么我们该怎么来添加一个管理员账户呢? 如果我们输入错误的Tomcat管 ...
- CentOS下配置SMTP
在服务器上配置一个SMTP邮件服务可能是在日常工作中经常会遇到的需要,比如在做一些简单测试的时候. 配置步骤无比简单,废话不说: 1,yum -y install mail 2,编辑/etc/mail ...
- 关于JS历史拓展
js由来 95年那时,绝大多数因特网用户都使用速度仅为28.8kbit/s 的“猫”(调制解调器)上网,但网页的大小和复杂性却不断增加.为完成简单的表单验证而频繁地与服务器交换数据只 ...
- 关于为什么RAID5往往掉一个盘后第二个盘也立刻挂掉的原因分析
很多人遇到过服务器RAID5挂掉,往往掉一个盘后,第二个盘也立刻挂掉. 大家都知道RAID5 一次允许一个盘缺失, RAID 5也是以数据的校验位来保证数据的安全,但它不是以单独硬盘来存放数据的校验位 ...
- DataGrid 简单数据绑定实例1
1.默认数据显示(自动显示列) 后台绑定 //DataGrid 数据绑定 dataGridOne.ItemsSource = _Context.Info.ToList(); 前台定义 <Data ...
- SVN 密码破解,svn密码本地找回 忘记密码
svn 密码被保存在本地文件中 C:\Users\[your computer name]\AppData\Roaming\Subversion\auth\svn.simple 文件下. 加密保存 到 ...
- iOS开发之17个常用代码整理
http://www.cnblogs.com/ios8/p/ios-17-code.html
- foreach遍历扩展(二)
一.前言 假设存在一个数组,其遍历模式是根据索引进行遍历的:又假设存在一个HashTable,其遍历模式是根据键值进行遍历的:无论哪种集合,如果它们的遍历没有一个共同的接口,那么在客户端进行调用的时候 ...
- (转)Oracle Data Guard配置
data guard配置的条件1.在主库和从库的所有机器上必须安装同一个版本的Oracle企业版.2.主库必须运行在归档模式下.3.主库和从库的操作系统必须一样(允许版本不同),从库可以使用与主库不同 ...