/* ' ' 文件名 免费天气插件j2ee+jqeury版本 ' 作者 烟de傀儡 ' 日期 2011年05月10日 ' 声明 ' 感谢 '走过四季' 提供jqeury版本的天气预报插件 ' 由于浏览器兼容问题(有些浏览器不能跨域抓数据) ' 我将此该为struts2的j2ee版本 ' */ //jsCity、jsWeather为您html中的元素ID function setWeather(tID,tCity,tTip,tWendu,tFengli) { alert(tWendu); $("#jsCity").html("<a href='http://www.weather.com.cn/html/weather/"+tID+".shtml' target=_bank>"+tCity+"</a>"); $("#jsWeather").html(tTip); $("#jsFengli").html(tFengli); $("#jsWendu").html(tWendu); } var cityid,weaXML,weaHTML; weaXML = "http://service.weather.com.cn/plugin/"; weaHTML = "http://m.weather.com.cn/data/"; $().ready(function() { cityid = $.cookies.get('wea_cityip'); if(cityid==null){ LoadJS('http://61.4.185.48:81/g/', function() { if (typeof id != 'undefined') { $.cookies.set('wea_cityip', id,null); cityid = $.cookies.get('wea_cityip'); getWeather(cityid); } }); } else{ getWeather(cityid); } }); function getWeather(cid) { $.cookies.set('wea_cityid', cid,null); var weajs = weaHTML+cid+'.html'; var url = "http://localhost:8080/weatherDemo/getWeatherData?url="+weajs; $.getJSON( //weajs, url, function(objJson){ var cityname = objJson.weatherinfo.city; //上海 var id = objJson.weatherinfo.cityid; //101020100 var cityinfo1=objJson.weatherinfo.weather1; //晴转多云 var cityinfo2=objJson.weatherinfo.weather2; var wd1=objJson.weatherinfo.wind1; //北风3-4级 var wd2=objJson.weatherinfo.wind2; var fl1=objJson.weatherinfo.fl1; //3-4级 var fl2=objJson.weatherinfo.fl2; var temp1=objJson.weatherinfo.temp1; //4℃~-1℃ var temp2=objJson.weatherinfo.temp2; var img1=objJson.weatherinfo.img1; var img2=objJson.weatherinfo.img2; var img3=objJson.weatherinfo.img3; var img4=objJson.weatherinfo.img4; var index=objJson.weatherinfo.index; var index_d=objJson.weatherinfo.index_d; var index_xc=objJson.weatherinfo.index_xc; var index_uv=objJson.weatherinfo.index_uv; var date=objJson.weatherinfo.date; var date_y=objJson.weatherinfo.date_y; var imgtitle1=objJson.weatherinfo.img_title1; var imgtitle2=objJson.weatherinfo.img_title2; var imgsingle=objJson.weatherinfo.img_single; var imgtitlesingle=objJson.weatherinfo.img_title_single; setWeather(id,cityname,cityinfo1,temp1.toString(),wd1); }); } function LoadJS(jsUrl,fCallBack) { var _script = document.createElement('script'); _script.setAttribute('type', 'text/javascript'); _script.setAttribute('charset', 'UTF-8'); _script.setAttribute('src', jsUrl); document.getElementsByTagName('head')[0].appendChild(_script); if(typeof fCallBack != 'undefined') { if ($.browser.msie){ _script.onreadystatechange = function() { if (this.readyState=='loaded'||this.readyState=='complete'){ fCallBack(); } }; }else if ($.browser.mozilla) { _script.onload = function(){ fCallBack(); }; }else{ fCallBack(); } } }
上面为:chWeather.js
package com.ch.util; import java.io.IOException; import org.apache.commons.httpclient.HttpClient; import org.apache.commons.httpclient.HttpException; import org.apache.commons.httpclient.HttpMethod; import org.apache.commons.httpclient.methods.GetMethod; public class DataHttpClient { public String getWeaherData(String url){ HttpClient http = new HttpClient(); if(null == url || url.equals("")){ url="http://m.weather.com.cn/data/101110101.html";//默认西安的天气 } HttpMethod method = new GetMethod(url); String data = ""; try { http.executeMethod(method); System.out.println(method.getStatusLine()); int i = method.getStatusCode(); if(vilidateStatus(i)){ data = method.getResponseBodyAsString(); } System.out.println("我来看看"+data); } catch (HttpException e) { System.out.println(e.getMessage()); return null; } catch (IOException e) { System.out.println(e.getMessage()); return null; } // 打印服务器返回的状态 return data; } public boolean vilidateStatus(int i){ if(i==200){ return true; } return false; } }
上面为:DataHttpClient.java
另外附上源码