实例介绍
【实例简介】
【实例截图】
【核心代码】
using 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
}
}
好例子网口号:伸出你的我的手 — 分享!
小贴士
感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。
- 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
- 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
- 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
- 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。
关于好例子网
本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明


网友评论
我要评论