Beispiel #1
0
        //请求网页获取天气信息
        public WebRequestWeatherInfo GetWeatherInfo(string city)
        {
            //通过聚合数据获取信息
            string http = "http://v.juhe.cn/weather/index?";
            string tmp  = "cityname=" + city + "&format=2";

            http += (tmp + "&key=4f2e721fdb5f8b778095e80ed0ffe104");

            Encoding       encoding       = Encoding.UTF8;
            HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create(http);

            httpWebRequest.ContentType = "application/json";
            httpWebRequest.Method      = "GET";
            httpWebRequest.Timeout     = 20000;

            HttpWebResponse httpWebResponse = (HttpWebResponse)httpWebRequest.GetResponse();
            StreamReader    streamReader    = new StreamReader(httpWebResponse.GetResponseStream(), Encoding.GetEncoding("utf-8"));
            string          responseContent = streamReader.ReadToEnd();

            httpWebResponse.Close();
            streamReader.Close();

            //json反序列化
            WebRequestWeatherInfo weatherinfo = JsonConvert.DeserializeObject <WebRequestWeatherInfo>(responseContent);

            return(weatherinfo);
        }
Beispiel #2
0
        internal void ShowDialog(string city)
        {
            this.panel.Controls.Clear();
            showTodayControl.Dock = DockStyle.Fill;
            this.panel.Controls.Add(showTodayControl);

            if (GlobalWeatherDict.Citypict.ContainsKey(city))
            {
                int tmp = GlobalWeatherDict.citypict[city];
                this.BackgroundImage = citypic_imageList.Images[tmp];
                ImageLayout layout = ImageLayout.Stretch;
                this.BackgroundImageLayout = layout;
            }
            else
            {
                int tmp = GlobalWeatherDict.citypict["暂无"];
                this.BackgroundImage = citypic_imageList.Images[tmp];
                ImageLayout layout = ImageLayout.Stretch;
                this.BackgroundImageLayout = layout;
            }

            //进行天气查询,并且存储在WeatherInfo类中
            WeatherInfo = WeatherInfo.GetWeatherInfo(city);

            //错误返回判断
            if (WeatherInfo.error_code == "0")
            {
                //显示当天天气
                showTodayControl.ShowWeatherInfo(WeatherInfo);
                //显示连续七天天气
                //showFivedaysControl.showfive(WeatherInfo);

                this.ShowDialog();//如果不支持要查询的城市就不显示
            }
            else if (WeatherInfo.error_code == "203902")
            {
                MessageBox.Show("查询不到该城市");
            }
            else
            {
                MessageBox.Show("查询出错,请重试");
            }
        }