java 天气预报

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>
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值