Java学习

java,spring,structs,hibernate,jsf,ireport,jfreechart,jasperreport,tomcat,jboss -----本博客已经搬家了,新的地址是 http://www.javaly.cn 如果有对文章有任何疑问或者有任何不懂的地方,欢迎到www.javaly.cn (Java乐园)指出,我会尽力帮助解决。一起进步

 

本站 JAVA 天气预报实例

大致步骤是
1 利用soap向webservice endpoint进行请求,取回请求结果
2 把结果显示在web界面上,web界面采用Java+Jsp
一 WeatherReport类
方法 1 构造soap请求(请求格式请见上面的链接),用用户输入的城市名称镶在此请求里面

java 代码

/**
* 获取SOAP的请求头,并替换其中的标志符号为用户输入的城市
*
* 编写者:王景辉
*
* @param city
* 用户输入的城市名称
* @return 客户将要发送给服务器的SOAP请求
*/
private static String getSoapRequest(String city) {
StringBuilder sb = new StringBuilder();
sb
.append("<?xml version=\"1.0\" encoding=\"utf-8\"?>"
+ "<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/\">"
+ "<soap:Body> <getWeatherbyCityName xmlns=\"http://WebXml.com.cn/\">"
+ "<theCityName>" + city
+ "</theCityName> </getWeatherbyCityName>"
+ "</soap:Body></soap:Envelope>");
return sb.toString();
}


方法 2 向endpoint发送上述SOAP请求,并设置一些请求属性,返回一个服务器端的InputStream(XML文档流)

java 代码

/**
* 用户把SOAP请求发送给服务器端,并返回服务器点返回的输入流
*
* 编写者:王景辉
*
* @param city
* 用户输入的城市名称
* @return 服务器端返回的输入流,供客户端读取
* @throws Exception
*/
private static InputStream getSoapInputStream(String city) throws Exception {
try {
String soap = getSoapRequest(city);
if (soap == null) {
return null;
}
URL url = new URL(
"http://www.webxml.com.cn/WebServices/WeatherWebService.asmx");
URLConnection conn = url.openConnection();
conn.setUseCaches(false);
conn.setDoInput(true);
conn.setDoOutput(true);

conn.setRequestProperty("Content-Length", Integer.toString(soap
.length()));
conn.setRequestProperty("Content-Type", "text/xml; charset=utf-8");
conn.setRequestProperty("SOAPAction",
"http://WebXml.com.cn/getWeatherbyCityName");

OutputStream os = conn.getOutputStream();
OutputStreamWriter osw = new OutputStreamWriter(os, "utf-8");
osw.write(soap);
osw.flush();
osw.close();

InputStream is = conn.getInputStream();
return is;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}


方法 3 解析方法2返回的XML文档流,并用特定的符号分隔,以便我们在Jsp页面进行结果分析

java 代码

/**
* 对服务器端返回的XML进行解析
*
* 编写者:王景辉
*
* @param city
* 用户输入的城市名称
* @return 字符串 用,分割
*/
public static String getWeather(String city) {
try {
Document doc;
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
dbf.setNamespaceAware(true);
DocumentBuilder db = dbf.newDocumentBuilder();
InputStream is = getSoapInputStream(city);
doc = db.parse(is);
NodeList nl = doc.getElementsByTagName("string");
StringBuffer sb = new StringBuffer();
for (int count = 0; count < nl.getLength(); count++) {
Node n = nl.item(count);
if(n.getFirstChild().getNodeValue().equals("查询结果为空!")) {
sb = new StringBuffer("#") ;
break ;
}
sb.append(n.getFirstChild().getNodeValue() + "#\n");
}
is.close();
return sb.toString();
} catch (Exception e) {
e.printStackTrace();
return null;
}
}

二 weatherInfo.jsp页面

核心功能是解析 方法3 所返回的字符串,向endpoint进行请求时,一个XML文档片段是

xml 代码


<?xml version="1.0" encoding="utf-8" ?>
<ArrayOfString xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://WebXml.com.cn/">
<string>湖南</string>
<string>长沙</string>
<string>57687</string>
<string>57687.jpg</string>
<string>2007-12-26 14:35:26</string>
<string>7℃ / 6℃</string>
<string>12月26日 小雨</string>
<string>西北风<=3级</string>
<string>7.gif</string>
<string>7.gif</string>
<string>今日天气实况:多云;7.4℃;风向/风力:西北风2级;空气质量:较差;紫外线强度:最弱</string>
<string>穿衣指数:感冒指数:温度较低,…… 请您在旅游时注意增加衣物。</string>
<string>8℃ / 5℃</string>
<string>12月27日 小雨</string>
<string>西北风<=3级</string>
<string>7.gif</string>
<string>7.gif</string>
<string>10℃ / 4℃</string>
<string>12月28日 小雨</string>
<string>西北风<=3级</string>
<string>7.gif</string>
<string>7.gif</string>
<string>长沙市位于湖南省东部偏北, …… 市区年均降水量1361.6毫米。……<string>
</ArrayOfString>


在Jsp中解析的代码如下,基本上是对字符串的操作,截取及截取长度的控制

java 代码

//穿衣指数
s1 = str.substring(str.indexOf("穿衣指数:"),str.indexOf("穿衣指数:")+4) ;
s1Content = str.substring(str.indexOf("穿衣指数:")+5,str.indexOf("感冒指数:")) ;
//感冒指数
s2 = str.substring(str.indexOf("感冒指数:"),str.indexOf("感冒指数:")+4) ;
s2Content = str.substring(str.indexOf("感冒指数:")+5,str.indexOf("晨练指数:")) ;

//晨练指数
s3 = str.substring(str.indexOf("晨练指数:"),str.indexOf("晨练指数:")+4) ;
s3Content = str.substring(str.indexOf("晨练指数:")+5,str.indexOf("交通指数:")) ;
//交通指数
s7 = str.substring(str.indexOf("交通指数:"),str.indexOf("交通指数:")+4) ;
s7Content = str.substring(str.indexOf("交通指数:")+5,str.indexOf("中暑指数:")) ;
//中暑指数
s4 = str.substring(str.indexOf("中暑指数:"),str.indexOf("中暑指数:")+4) ;
s4Content = str.substring(str.indexOf("中暑指数:")+5,str.indexOf("防晒指数:")) ;
//防晒指数
s5 = str.substring(str.indexOf("防晒指数:"),str.indexOf("防晒指数:")+4) ;
s5Content = str.substring(str.indexOf("防晒指数:")+5,str.indexOf("旅行指数:")) ;
//旅行指数
s6 = str.substring(str.indexOf("旅行指数:"),str.indexOf("旅行指数:")+4) ;
s6Content = str.substring(str.indexOf("旅行指数:")+5) ;


运行附件:http://localhost:8080/yourProject/tianqi.jsp

好了,基本上核心代码就是上边那些了!不仅如此,加入我们想要在自己的系统里加入飞机票,火车票,股票信息等等之类的功能,只要有相应的webservice,我们都可以实现

文档来源 http://mhqawjh.javaeye.com/blog/150954

源代码 点击下面现在下载按钮

这里感谢 技工窝 mhqawjh 博客地址 http://mhqawjh.javaeye.com/

posted on 2009-04-08 14:25 找个美女做老婆 阅读(1844) 评论(2)  编辑  收藏

评论

# re: 本站 JAVA 天气预报实例 2012-12-25 11:20 dgqjava

真心很垃圾~  回复  更多评论   

# re: 本站 JAVA 天气预报实例 2013-12-17 11:59 梁建民

很好很实用  回复  更多评论   


只有注册用户登录后才能发表评论。


网站导航:
 

导航

统计

公告

本blog已经搬到新家了, 新家:www.javaly.cn
 http://www.javaly.cn

常用链接

留言簿(6)

随笔档案

文章档案

搜索

最新评论

阅读排行榜

评论排行榜