使用WSDL解析天气
1、去webxml网址找到天气的连接:
1-1:获取连接,以及查看相对应的方法:
1-2:由于解析的时候,无法将链接成功解析出来,所以,我需要将链接存到本地电脑上,然后再进行解析:在网页链接后面输入:?wsdl,然后另存到本地去:
1-3:由于无法解析,可以把另存出来的文件进行更改:
把<s:element ref=“s:schema”/><s:any/> 改为: <s:any minOccurs=“2” maxOccurs=“2”/>
注意:是文件里面所有的<s:element ref=“s:schema”/><s:any/> 都得替换,记得一个有三个
1-4:在命令窗口输入:D:\myDome\weather\WeatherWS.asmx.xml这个是你另存到本地的文件路径,然后就可以直接解析成功了!
wsimport -keep D:\myDome\weather\WeatherWS.asmx.xml
1-5:解析出来后 的src目录下就有以下文件:
1-6:进行一波简单的测试操作:
public static void main(String[] args) {
String CityCode = "长沙";
WeatherWS weatherWS = new WeatherWS();
WeatherWSSoap weatherWSSoap = weatherWS.getWeatherWSSoap();
ArrayOfString weather = weatherWSSoap.getWeather(CityCode, null);
System.out.println(weather.getString());
}
2:放入页面测试:
2-1:后台Action代码:
public String getWeather() {
//获取城市
String city = request.getParameter("city");
String CityCode= "";
if(null == city || "".equals(city)) {//默认长沙
CityCode = "长沙";
}else {
CityCode=city;
}
WeatherWS weatherWS = new WeatherWS();
WeatherWSSoap weatherWSSoap = weatherWS.getWeatherWSSoap();
ArrayOfString weather = weatherWSSoap.getWeather(CityCode, null);
List<String> ls = weather.getString();
//将需要的信息存入session,放人jsp页面:
//获取省份
request.getSession().setAttribute("ccity", ls.get(0));
//城市
request.getSession().setAttribute("cname", ls.get(1));
//获取时间
request.getSession().setAttribute("ctime", ls.get(3));
//获取天气实况
request.getSession().setAttribute("cweather", ls.get(4));
// return "weather";
return "menu";
}
2-2:jsp页面代码:
做了搜索,只能按城市查找
<form action="/pro_crm/sy/sysUserAction_getWeather.action" method="post">
今日天气:
省份: ${ccity }
城市: <input type="text" name="city" value="${cname }"> <input type="submit" value="搜索">
时间:${ctime }
${cweather }
${atmosphere}
</form>
2-3:页面效果:
以上就是一个简单的天气预报出来了