基本信息
源码名称:C# 获取天气信息
源码大小:0.95M
文件格式:.zip
开发语言:C#
更新时间:2017-07-20
   友情提示:(无需注册或充值,赞助后即可获取资源下载链接)

     嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):78630559

本次赞助数额为: 1 元 
   源码介绍

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     



    }
}