·您当前的位置:首页 > 技术教程 > JavaScript >

[JS]天气预报服务含PM2.5,紫外线指数

时间:2016-01-13 15:15酷播
[JS]天气预报服务含PM2.5,紫外线指数

接口实现的程序代码片断

  1. try 
  2.             { 
  3.                 string jsoncallback = Request["jsoncallback"]; 
  4.                 string ip = getClientIp(); 
  5.                 string[] ipips = ip.Split(','); 
  6.  
  7.                 ip = ips[ips.Length - 1]; 
  8.                 ip = String.IsNullOrEmpty(Request["ip"]) ? ip : Request["ip"]; 
  9.  
  10.                 ///获取详细地址 
  11.                 string area = IPLocation.IPLocate(_helper.GetQQWryDataPath(), ip); 
  12.                  
  13.                 ///根据地址分析出省 
  14.                 Regex zzq = new Regex(String.Format("({0})", _helper.GetProvinces())); 
  15.                 Match mzzq = zzq.Match(area); 
  16.                  
  17.                 ///根扬地址分析出所在地区 
  18.                 Regex shi = new Regex(String.Format("({0})", _helper.GetCityNames())); 
  19.                 Match mshi = shi.Match(area); 
  20.  
  21.                 SortedList citys = new SortedList(); 
  22.  
  23.                 object o = _cached.Get("citys"); 
  24.                 if (null != o) 
  25.                 { 
  26.                     citys = o as SortedList; 
  27.                 } 
  28.                 else 
  29.                 { 
  30.                     string[] cityCodes = _helper.GetCityCodes().Split('|'); 
  31.                     foreach (string city in cityCodes) 
  32.                     { 
  33.                         string[] c = city.Split('-'); 
  34.                         if (!citys.ContainsKey(c[0])) 
  35.                             citys.Add(c[0], c[1]); 
  36.                     } 
  37.                     _cached.Set("citys", citys); 
  38.                 } 
  39.                 string weatId = "1"
  40.  
  41.                 ///是否存在省的天气 
  42.                 weatId = citys.ContainsKey(mzzq.Value) ? Convert.ToString(citys[mzzq.Value]) : weatId; 
  43.                 ///是否存在市的天气 
  44.                 weatId = citys.ContainsKey(mshi.Value) ? Convert.ToString(citys[mshi.Value]) : weatId; 
  45.  
  46.                 string keyName = String.Format("weather_{0}", weatId); 
  47.                 o = _cached.Get(keyName); 
  48.                 resultJson result = null
  49.                 if (null == o) 
  50.                 { 
  51.                     TY.Web web = new TY.Web(); 
  52.  
  53.                     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); 
  54.  
  55.                     result = JsonConvert.DeserializeObject<resultJson>(ret); 
  56.                     _cached.Set(keyName, result, 30); 
  57.                 } 
  58.                 else 
  59.                 { 
  60.                     result = o as resultJson; 
  61.                 } 
  62.                 Response.Write(jsoncallback + "({\"week\":\"" + result.result.week + "\", \"citynm\":\"" + result.result.citynm + "\", \"temp\":\"" + result.result.temperature + "\",\"weather\":\"" + result.result.weather + "\" });"); 
  63.             } 
  64.             catch (Exception ex) 
  65.             { 
  66.                 Log.WriteErrorLog("Tools.Web::weather", ex.Message); 
  67.             } 

 

热门文章推荐

请稍候...

保利威视云平台-轻松实现点播直播视频应用

酷播云数据统计分析跨平台播放器