提供本地天气预报服务,数据每20分钟更新一次,请点击天气预报,数据来源聊服务网
天气数据:
{"week":"星期四", "citynm":"上海", "temp":"20℃/15℃","weather":"中雨转多云", "air":"164", "sunriseTime":"05:24", "sunsetTime":"18:23", "Atm":"1010 hPa", "uv":"强", "rainfall":"70%", "cyzs":"温凉", "cyzs_desc":"一件羊毛衫、套装、夹克衫、西服套装、马甲衬衫+夹克衫配长裤年老体弱者:一件厚羊毛衫+夹衣或风衣 " }
获取本地天气的示例代码:
$.getJSON("http://tools.liaofuwu.com/weather.aspx?jsoncallback=?", function (json) {
$("#div_Result").html("<b>" + json.week + " " + json.citynm + " " + json.temp + " " + json.weather + "<b/><br/><br/>");
});
根据客户端IP获取本地天气的示例代码:
$.getJSON("http://tools.liaofuwu.com/weather.aspx?ip=180.154.13.153&jsoncallback=?", function (json) {
$("#div_Result").html("<b>" + json.week + " " + json.citynm + " " + json.temp + " " + json.weather + "<b/><br/><br/>");
});
接口实现的程序代码片断
try
{
string jsoncallback = Request["jsoncallback"];
string ip = getClientIp();
string[] ips = ip.Split(',');
ip = ips[ips.Length - 1];
ip = String.IsNullOrEmpty(Request["ip"]) ? ip : Request["ip"];
///获取详细地址
string area = IPLocation.IPLocate(_helper.GetQQWryDataPath(), ip);
///根据地址分析出省
Regex zzq = new Regex(String.Format("({0})", _helper.GetProvinces()));
Match mzzq = zzq.Match(area);
///根扬地址分析出所在地区
Regex shi = new Regex(String.Format("({0})", _helper.GetCityNames()));
Match mshi = shi.Match(area);
SortedList citys = new SortedList();
object o = _cached.Get("citys");
if (null != o)
{
citys = o as SortedList;
}
else
{
string[] cityCodes = _helper.GetCityCodes().Split('|');
foreach (string city in cityCodes)
{
string[] c = city.Split('-');
if (!citys.ContainsKey(c[0]))
citys.Add(c[0], c[1]);
}
_cached.Set("citys", citys);
}
string weatId = "1";
///是否存在省的天气
weatId = citys.ContainsKey(mzzq.Value) ? Convert.ToString(citys[mzzq.Value]) : weatId;
///是否存在市的天气
weatId = citys.ContainsKey(mshi.Value) ? Convert.ToString(citys[mshi.Value]) : weatId;
string keyName = String.Format("weather_{0}", weatId);
o = _cached.Get(keyName);
resultJson result = null;
if (null == o)
{
TY.Web web = new TY.Web();
string ret = web.getHtml(String.Format("http://api.k780.com:88/?app=weather.today&weaid={0}&appkey=10003&sign=b59bc3ef6191eb9f747dd4e83c99f2a4&format=json", weatId), TY.EnCoding.UTF8);
result = JsonConvert.DeserializeObject<resultJson>(ret);
_cached.Set(keyName, result, 30);
}
else
{
result = o as resultJson;
}
Response.Write(jsoncallback + "({\"week\":\"" + result.result.week + "\", \"citynm\":\"" + result.result.citynm + "\", \"temp\":\"" + result.result.temperature + "\",\"weather\":\"" + result.result.weather + "\" });");
}
catch (Exception ex)
{
Log.WriteErrorLog("Tools.Web::weather", ex.Message);
}
扫描二维码关注公众号,回复:
9003405 查看本文章