1. 可以在页面直接访问某个连接地址如:
http://weather.265.com/weather.htm
http://minisite.qq.com/Weather/news_new.html
http://weather.qq.com/inc/ss258.htm
http://news.sina.com.cn/iframe/weather/130101.html
2. 使用rss获取天气信息:
rss地址:http://weather.raychou.com/?/list/
下面以宜昌为例:http://weather.raychou.com/?/detail/57461/rss
注:实现代码比较简单主要为了说明用法
Weatheraction.java代码
package com.hw.msds.base.util;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import com.hw.msds.base.struts.BaseAction;
import com.hw.msds.realtimemonitor.businessimp.BaseParser;
public class WeatherAction extends BaseAction {
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
BaseParser bp = new BaseParser();
bp.setSite("http://weather.raychou.com/?/detail/57461/rss");
Document document = bp.getDocument();
StringBuffer sb = new StringBuffer();
NodeList title = document.getElementsByTagName("title");
if (title != null) {
int len = title.getLength();
if (len > 0) {
Node node = title.item(0);
sb.append(node.getTextContent() + ":");
}
}
NodeList description = document.getElementsByTagName("description");
if (description != null) {
int len = description.getLength();
if (len > 1) {
Node node = description.item(1);
sb.append(node.getTextContent());
}
}
printResponseText(response, sb.toString());
return null;
}
}
package com.hw.msds.base.util;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import com.hw.msds.base.struts.BaseAction;
import com.hw.msds.realtimemonitor.businessimp.BaseParser;
public class WeatherAction extends BaseAction {
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
BaseParser bp = new BaseParser();
bp.setSite("http://weather.raychou.com/?/detail/57461/rss");
Document document = bp.getDocument();
StringBuffer sb = new StringBuffer();
NodeList title = document.getElementsByTagName("title");
if (title != null) {
int len = title.getLength();
if (len > 0) {
Node node = title.item(0);
sb.append(node.getTextContent() + ":");
}
}
NodeList description = document.getElementsByTagName("description");
if (description != null) {
int len = description.getLength();
if (len > 1) {
Node node = description.item(1);
sb.append(node.getTextContent());
}
}
printResponseText(response, sb.toString());
return null;
}
}
配置信息代码
<action path="/weather" type="com.hw.msds.base.util.WeatherAction"></action>
<action path="/weather" type="com.hw.msds.base.util.WeatherAction"></action>
界面上我们使用jquery来发送请求,并获取天气信息:
Js代码
<script type="text/javascript" src="<%=basePath%>js/jquery-1.2.6.min.js"></script>
$(document).ready(function () {
$.get("<%=basePath%>weather.do", function (data) {
$('#weather').html(data);
});
});
<span id="weather">未获取到天气信息</span>
http://weather.265.com/weather.htm
http://minisite.qq.com/Weather/news_new.html
http://weather.qq.com/inc/ss258.htm
http://news.sina.com.cn/iframe/weather/130101.html
2. 使用rss获取天气信息:
rss地址:http://weather.raychou.com/?/list/
下面以宜昌为例:http://weather.raychou.com/?/detail/57461/rss
注:实现代码比较简单主要为了说明用法
Weatheraction.java代码
package com.hw.msds.base.util;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import com.hw.msds.base.struts.BaseAction;
import com.hw.msds.realtimemonitor.businessimp.BaseParser;
public class WeatherAction extends BaseAction {
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
BaseParser bp = new BaseParser();
bp.setSite("http://weather.raychou.com/?/detail/57461/rss");
Document document = bp.getDocument();
StringBuffer sb = new StringBuffer();
NodeList title = document.getElementsByTagName("title");
if (title != null) {
int len = title.getLength();
if (len > 0) {
Node node = title.item(0);
sb.append(node.getTextContent() + ":");
}
}
NodeList description = document.getElementsByTagName("description");
if (description != null) {
int len = description.getLength();
if (len > 1) {
Node node = description.item(1);
sb.append(node.getTextContent());
}
}
printResponseText(response, sb.toString());
return null;
}
}
package com.hw.msds.base.util;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import com.hw.msds.base.struts.BaseAction;
import com.hw.msds.realtimemonitor.businessimp.BaseParser;
public class WeatherAction extends BaseAction {
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
BaseParser bp = new BaseParser();
bp.setSite("http://weather.raychou.com/?/detail/57461/rss");
Document document = bp.getDocument();
StringBuffer sb = new StringBuffer();
NodeList title = document.getElementsByTagName("title");
if (title != null) {
int len = title.getLength();
if (len > 0) {
Node node = title.item(0);
sb.append(node.getTextContent() + ":");
}
}
NodeList description = document.getElementsByTagName("description");
if (description != null) {
int len = description.getLength();
if (len > 1) {
Node node = description.item(1);
sb.append(node.getTextContent());
}
}
printResponseText(response, sb.toString());
return null;
}
}
配置信息代码
<action path="/weather" type="com.hw.msds.base.util.WeatherAction"></action>
<action path="/weather" type="com.hw.msds.base.util.WeatherAction"></action>
界面上我们使用jquery来发送请求,并获取天气信息:
Js代码
<script type="text/javascript" src="<%=basePath%>js/jquery-1.2.6.min.js"></script>
$(document).ready(function () {
$.get("<%=basePath%>weather.do", function (data) {
$('#weather').html(data);
});
});
<span id="weather">未获取到天气信息</span>