找传奇、传世资源到传世资源站!

C# 获取天气信息

8.5玩家评分(1人评分)
下载后可评
介绍 评论 失效链接反馈

from clipboardusing System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Windows.Forms;using System.Threading;using System.Runtime.Serialization.Json;using System.Runtime.Serialization;using clsHttpClass;using Newtonsoft.Json;namespace iWeather{ public partial class Form1 : Form { public Thread ThreadTimerGetHttp; public bool bThreadTimerGetHttp; delegate void AppendStringCallback(int iType, byte[] text); //显示结果信息 AppendStringCallback appendstringcallback; string HttpUrl = string.Empty; string HttpTimeDiv = "200"; #region 路段结构 [DataContractAttribute] public class WeatherDetail { [DataMember] public string ID { set; get; } [DataMember] public string name { set; get; } [DataMember] public string status { set; get; } } public struct WeatherInfo { public string ID; public string Name; public string Status; } #endregion public class WeatherMessage { [DataMember] public String error { get;set; } [DataMember] public String status { get;set; } [DataMember] public String date { get; set;} [DataMember] public WeatherModel[]results{ get; set; } } public class WeatherModel { [DataMember] public String currentCity{ get; set; } [DataMember] public String pm25 { get; set;} [DataMember] public Weather_data[]weather_data { get; set; } } public class Weather_data { public String date { get; set;} public String dayPictureUrl { get; set; } public String nightPictureUrl { get; set; } public String weather { get;set; } public String wind { get; set;} public String temperature{ get; set; } } public Form1() { InitializeComponent(); appendstringcallback = new AppendStringCallback(AppendString); //显示结果 } private void btn_GetWeather_Click(object sender, EventArgs e) { //ThreadTimerGetHttp = new Thread(new ThreadStart(TimerGetHttp));//创建一个线程定时检测LED卡和环境检测卡的各种状态 //ThreadTimerGetHttp.IsBackground = true;//将线程设为后台运行 //ThreadTimerGetHttp.Start(); //bThreadTimerGetHttp = true; if (txt_WetherIO.Text.ToString().Trim() == null) { txt_WetherIO.Focus(); } else { String location = txt_WetherIO.Text.Trim(); WeatherHelper helper = new WeatherHelper(location); WeatherMessage weathermessage = helper.GetWeather(); String content = ""; if (weathermessage.status.Equals("success")) { WeatherModel weatherModel = weathermessage.results[0]; content = content weatherModel.currentCity "\r\n"; Weather_data[] weather_datas = weatherModel.weather_data; for (int i = 0; i <= weather_datas.Length - 1; i ) { content = content weather_datas[i].date "\r\n"; content = content weather_datas[i].weather "\r\n"; content = content weather_datas[i].temperature "\r\n"; content = content weather_datas[i].wind "\r\n"; } txt_list.Text = content; } else { txt_list.Text = "对不起,没有查询到你要查询的城市"; } } } public void AppendString(int iType, byte[] text) { try { if (txt_list.InvokeRequired == true) { this.Invoke(appendstringcallback, iType, text); } else { if (text == null) { Exception ex = new Exception("AppendString函数传入值text为null"); return; } if (txt_list.Text.Length > 30000) txt_list.Clear(); string ss = Encoding.UTF8.GetString(text); switch (iType) { case 1: #region 信息前添加日期 txt_list.AppendText(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") ss "\r\n"); break; #endregion case 2: #region 信息前不添加日期 txt_list.AppendText(ss "\r\n"); break; #endregion } } } catch (Exception ex) { } } #region 定时获取HTTP信息 void TimerGetHttp() { while (true) { while (bThreadTimerGetHttp) { Thread.Sleep(10); AppendString(1, Encoding.UTF8.GetBytes(" 开始循环获取:天气预报信息...")); try { //HttpUrl = "http://www.weather.com.cn/data/sk/101010100.html"; //HttpUrl = txt_WetherIO.Text; HttpUrl = "http://wthrcdn.etouch.cn/weather_mini?citykey=101020800"; string sRec =HttpClass.GetWebContent(HttpUrl); if (sRec.Trim() == "") AppendString(1, Encoding.UTF8.GetBytes(" 接收:" HttpUrl " 信息为空..")); sRec = sRec.Substring(1, sRec.Length - 2);//去掉前后的{} sRec = "[" sRec "]";//字符串末尾添加[]符号 //JSON前后需要添加[]字符,否则报错 var c = Newtonsoft.Json.JsonConvert.DeserializeObject<dynamic>(sRec); List<WeatherDetail> WeatherList = Newtonsoft.Json.JsonConvert.DeserializeObject<List<WeatherDetail>>(sRec); for (int j = 0; j < WeatherList.Count; j ) { } Thread.Sleep(100); } catch { AppendString(1, Encoding.UTF8.GetBytes(" 获取天气预报信息异常.."));//地磁服务器异常 Thread.Sleep(1000); continue; } #region 延时 int t = (1000 * 60 * Convert.ToInt32(HttpTimeDiv)) / 100;//间隔分钟数转换成0.1秒的次数 int p = 0; while (true) { if ((p > t) || (!bThreadTimerGetHttp)) break; Thread.Sleep(100); p = 1; } #endregion } Thread.Sleep(10); } } #endregion }}

评论

发表评论必须先登陆, 您可以 登陆 或者 注册新账号 !


在线咨询: 问题反馈
客服QQ:174666394

有问题请留言,看到后及时答复