天气预报功能
先看看在网上找到的代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 | function getWeather() { //request get current ip $.ajax({url: 'http://61.4.185.48:81/g/', dataType: "script", success: function(result){ var weatherUrl; //Current city url var provinceCode; //Current province code var cityCode; //Current city code var H_CITY = ["北京", "上海", "天津", "重庆"]; if (ip){ var new_ip = ip.replace("_", "").replace("_", ""); //according to current ip get current city detail info $.ajax({url: ('http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=js&ip=' + new_ip), dataType: "script", success: function(_result){ if (remote_ip_info.ret == '1'){ //get current province code provinceCode = getWeatherCode("http://m.weather.com.cn/data5/city.xml", remote_ip_info.province, 2); //get current province code cityCode = getWeatherCode("http://m.weather.com.cn/data5/city" + provinceCode + ".xml", remote_ip_info.city, 4); //get current weather url weatherUrl = "http://m.weather.com.cn/data/101" + cityCode + (H_CITY.indexOf(remote_ip_info.city)!=-1?"00":"01") + ".html"; queryWeather(weatherUrl); } else { $("#weatherContent").html("没有找到匹配的 IP 地址信息!"); } }, error: error, timeout: "3000"}); } else { $("#weatherContent").html("没有找到IP信息!"); } }, error: error, timeout: "3000"}); } //find weather info function queryWeather(url) { $.ajax({ url: url, data: null, success: success, error: error, dataType: "json", timeout: "3000" }); } //get city weather code function getWeatherCode(url, address, length) { var str = $.ajax({url: url, async: false}).responseText; var index = str.indexOf(address); if(str && index == -1) { alert("没有对应城市信息!"); } else { return str.substring(index - (length + 1), index - 1); } } var success = function(response){ alert("dddddd"); parseToday(response.weatherinfo); }; var error = function(xhr, status) { alert("Http 请求错误!"); }; var parseToday = function(weatherInfo){ //信息显示 alert("=============" + weatherInfo); } |
但这种通过JS的方式,不可行,因为存在跨域问题,所以按照这个思路,在后台服务端实现.为方便,直接用jsp实现,新建weather.jsp,加入代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | try { String citycode = request.getParameter("citycode"); StringBuffer sb = new StringBuffer(); URL url = null; InputStream input = null; //现在是通过城市编码来查询天气,编码是调用处传到后台来,也可以通过其它接口服务来自动去查询,如后面给出的思路 if (citycode == null || "".equals(citycode.trim())) { sb.append("error"); } else { url = new URL("http://m.weather.com.cn/data/" + citycode.trim() + ".html");//此处为地区编码 input = url.openStream(); int i = -1; byte[] b = new byte[1024]; while ((i = input.read(b)) != -1) { sb.append(new String(b, 0, i)); } input.close(); } out.println(sb.toString()); } catch (Exception ex) { out.println("error"); } |
可以使用最上面JS参考代码中的接口自动得到客户的城市编码
http://whois.pconline.com.cn/ 得到IP
http://pv.sohu.com/cityjson 得到IP
html中加入,便可在js里取得IP,alert(returnCitySN.cip);
(自动得到IP和城市编码方式,后面再实现)
现在html中加入iframe,citycode=101040100就是你要查询天气的城市编码,可在这里查询http://www.wsxa.com/WebService/WeatherI.asp
1 2 3 4 5 6 7 8 9 10 | <iframe id="WeatherFrame" name="WeatherFrame" src="weather.jsp?citycode=101040100" style="width: 0px;height: 0px;border: 0px;" onload="initWeather()"></iframe> function initWeather(){ var str = $(window.frames["WeatherFrame"].document).find("body").html(); if(str != null && str != "error"){ var weather = string2json(str).weatherinfo; alert(weather.temp1); alert(weather.city); } } |
weatherinfo对象说明:
{
"weatherinfo" : {
"city" : "成都",
"city_en" : "chengdu",
"date_y" : "2013年9月13日",
"date" : "",
"week" : "星期五",
"fchh" : "11",
"cityid" : "101270101",
"temp1" : "30℃~19℃", //从今天开始到第六天的每天的天气情况,这里的温度是摄氏温度
"temp2" : "30℃~20℃",
"temp3" : "30℃~21℃",
"temp4" : "28℃~21℃",
"temp5" : "27℃~22℃",
"temp6" : "26℃~20℃",
"tempF1" : "86℉~66.2℉", //从今天开始到第六天的每天的天气情况,这里的温度是华氏温度
"tempF2" : "86℉~68℉",
"tempF3" : "86℉~69.8℉",
"tempF4" : "82.4℉~69.8℉",
"tempF5" : "80.6℉~71.6℉",
"tempF6" : "78.8℉~68℉",
"weather1" : "多云", //今天天气描述
"weather2" : "多云",
"weather3" : "多云转阴",
"weather4" : "阵雨转阴",
"weather5" : "小雨转小到中雨",
"weather6" : "小到中雨转阵雨",
"img1" : "1", //天气描述图片序号
"img2" : "99",
"img3" : "1",
"img4" : "99",
"img5" : "1",
"img6" : "2",
"img7" : "3",
"img8" : "2",
"img9" : "7",
"img10" : "21",
"img11" : "21",
"img12" : "3",
"img_single" : "1",
"img_title1" : "多云", //图片名称
"img_title2" : "多云",
"img_title3" : "多云",
"img_title4" : "多云",
"img_title5" : "多云",
"img_title6" : "阴",
"img_title7" : "阵雨",
"img_title8" : "阴",
"img_title9" : "小雨",
"img_title10" : "小到中雨",
"img_title11" : "小到中雨",
"img_title12" : "阵雨",
"img_title_single" : "多云",
"wind1" : "南风小于3级", //风速描述
"wind2" : "南风小于3级",
"wind3" : "南风小于3级",
"wind4" : "南风小于3级",
"wind5" : "北风小于3级",
"wind6" : "北风小于3级",
"fx1" : "南风",
"fx2" : "南风",
"fl1" : "小于3级", //风速级别描述
"fl2" : "小于3级",
"fl3" : "小于3级",
"fl4" : "小于3级",
"fl5" : "小于3级",
"fl6" : "小于3级",
"index" : "热", //今天穿衣指数
"index_d" : "天气热,建议着短裙、短裤、短薄外套、T恤等夏季服装。",
"index48" : "热", //48小时穿衣指数
"index48_d" : "天气热,建议着短裙、短裤、短薄外套、T恤等夏季服装。",
"index_uv" : "中等", //紫外线及48小时紫外线
"index48_uv" : "中等",
"index_xc" : "适宜", //洗车
"index_tr" : "适宜", //旅游
"index_co" : "较不舒适", //舒适指数
"st1" : "30",
"st2" : "19",
"st3" : "30",
"st4" : "20",
"st5" : "30",
"st6" : "20",
"index_cl" : "适宜", //晨练
"index_ls" : "适宜", //晾晒
"index_ag" : "较易发" //过敏
}
}
Comments are currently closed.