简体   繁体   English

雅虎天气预报C#

[英]Yahoo weather Forecast c#

Looking for Getting current weather and weather forecast for London woeid 44418 https://query.yahooapis.com/v1/public/yql? 寻找有关伦敦woeid 44418的当前天气和天气预报https://query.yahooapis.com/v1/public/yql? q=select%20*%20from%20weather.forecast%20where%20woeid%20in%20(select%20woeid%20from%20geo.places(1)%20where%20woeid%20%3D044418)&format=xml&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys q =从%20weather.select%20woeid%20in%20中选择%20 *%20%(选择从%20geo.places(1)%20where%20woeid%20%3D044418选择%20woeid%20%)&format = xml&env = store%3A% 2F%2Fdatatables.org%2Falltableswithkeys

How to get forecast for this ? 如何获得对此的预测? Following is code for weather today . 以下是今天的天气代码。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Xml;
using MySql.Data.MySqlClient;
using System.Windows.Forms.DataVisualization.Charting;
using System.Runtime.InteropServices;


namespace weatherstation1
{
    public partial class Form2 : Form
    {

        string Town;
        string Temperature;
        string Temperature2;
        string Condition;
        string Humidity;
        string Windspeed;
        string ConditionCode;
        int cityNumber = 44418;

        public Form2()
        {
            InitializeComponent();
            Getweather();
            GetCondition();
            textBox1.Text = Town;
            textBox3.Text = Condition;
            textBox4.Text = Humidity;
            textBox5.Text = Windspeed;


        }

        private void toolStripLabel1_Click(object sender, EventArgs e)
        {

        }

        private void aboutToolStripMenuItem_Click(object sender, EventArgs e)
        {
            AboutBox1 frmAbout = new AboutBox1();
            frmAbout.Show();
        }

        private void Form2_Load(object sender, EventArgs e)
        {

        }

        private void notifyIcon1_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            this.Show();
        }

        private void contextMenuStrip1_Opening(object sender, CancelEventArgs e)
        {

        }

        private void aboutToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            AboutBox1 frmAbout = new AboutBox1();
            frmAbout.Show();
        }


        private void Getweather()
        {

            string query = String.Format("https://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20weather.forecast%20where%20woeid%20in%20(select%20woeid%20from%20geo.places(1)%20where%20woeid%20%3D044418)&format=xml&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys");

            XmlDocument wData = new XmlDocument();
            wData.Load(query);

            XmlNamespaceManager manager = new XmlNamespaceManager(wData.NameTable);
            manager.AddNamespace("yweather", "http://xml.weather.yahoo.com/ns/rss/1.0");
            XmlNode channel = wData.SelectSingleNode("rss").SelectSingleNode("channel");
            XmlNodeList nodes = wData.SelectNodes("/rss/channel/item/yweather:forecast", manager);

            Temperature = channel.SelectSingleNode("item").SelectSingleNode("yweather:condition", manager).Attributes["temp"].Value;
            Temperature2 = ((Double.Parse(Temperature) - 32) / 1.8).ToString();
            Condition = channel.SelectSingleNode("item").SelectSingleNode("yweather:condition", manager).Attributes["text"].Value;
            ConditionCode = channel.SelectSingleNode("item").SelectSingleNode("yweather:condition", manager).Attributes["code"].Value;
            Time = channel.SelectSingleNode("item").SelectSingleNode("yweather:condition", manager).Attributes["date"].Value;
           // Windspeed = channel.SelectSingleNode("yweather:wind", manager).Attributes["speed"].Value;
           // Town = channel.SelectSingleNode("yweather:location", manager).Attributes["city"].Value;




        }

        private void database()
        {
            // string Time = DateTime.Now.ToString("d/M/yyyy HH:mm");
            string MyConnection = "datasource=localhost;database=weatherstation;port=3306;username=weatherstation;password=123456";
            MySqlConnection MyConn = new MySqlConnection(MyConnection);
            string query1 = "INSERT INTO weatherstation1.London"+
                                            "(City, Time, Temperature, Temperature2, WeatherCondition) VALUES('" + Town +
                                             "','" + Time + "','" + Temperature + "','" + Temperature2 + "' + Condition + "');";
            MySqlCommand MyCommand = new MySqlCommand(query1, MyConn);
            MySqlDataReader MyReader;
            MyConn.Open();
            MyReader = MyCommand.ExecuteReader();





        }

        public void GetCondition()
        {
            string cc = ConditionCode;
            if (cc <= 'Showers')
            {
                weatherCondition.ImageLocation = "showers.png";
            }
            else if (cc <= 'Scattered Showers')
            {
                weatherCondition.ImageLocation = "scatteredshowers.png";
            }
            else if (cc  <= 'Mostly Cloudy')
            {
                weatherCondition.ImageLocation = "mostlycloudy.png";
            }
            else if (cc <= 'Rain')
            {
                weatherCondition.ImageLocation = "rain.png";
            }

            else if (cc <= 'Mostly Cloudy')
            {
                weatherCondition.ImageLocation = "mostlycloudy.png";
            }
            else if (cc <= 'Rain')
            {
                weatherCondition.ImageLocation = "rain.png";
            }
            else if (cc >= 'Scattered Thunderstorms')
            {
                weatherCondition.ImageLocation = "scatteredthunderstorms.png";
            }


        }

        private void button1_Click(object sender, EventArgs e)
        {


        }

        private void button1_Click_1(object sender, EventArgs e)
        {
            Getweather();
            textBox1.Text = Town;
            textBox3.Text = Condition;
            //textBox4.Text = Humidity;
            //textBox5.Text = Windspeed;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            database();     
        }

        private void closeToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void openToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.WindowState = FormWindowState.Maximized;
        }

        private void refreshToolStripMenuItem_Click(object sender, EventArgs e)
        {
            database();
        }

        private void tabPage1_Click(object sender, EventArgs e)
        {

        }

        private void graph()
        {
            if (radioButton2.Checked)
            {
                MySqlDataReader MyReader2;
                string MyConnection = "datasource=localhost;database=weatherstation;port=3306;username=weatherstation;password=123456";
                MySqlConnection MyConn2 = new MySqlConnection(MyConnection);
                MyConn2.Open();
                string query2 = " SELECT * FROM  London   ";
                MySqlCommand MyCommand2 = new MySqlCommand(query2, MyConn2);
                MyReader2 = MyCommand2.ExecuteReader();

                while (MyReader2.Read())
                {
                    chart1.Series["Series1"].Points.AddXY(MyReader2.GetString("Time"), MyReader2.GetString("Temperature"));
                    chart1.Series["Series1"].ChartType = SeriesChartType.FastLine;
                    chart1.Series["Series1"].Color = Color.Blue;
                }
            }
            if (radioButton1.Checked)
            {

                MySqlDataReader MyReader2;
                string MyConnection = "datasource=localhost;database=weatherstation;port=3306;username=weatherstation;password=123456";
                MySqlConnection MyConn2 = new MySqlConnection(MyConnection);
                MyConn2.Open();
                string query2 = " SELECT * FROM  London ";
                MySqlCommand MyCommand2 = new MySqlCommand(query2, MyConn2);
                MyReader2 = MyCommand2.ExecuteReader();
                while (MyReader2.Read())
                {
                    chart1.Series["Series1"].Points.AddXY(MyReader2.GetString("Time"), MyReader2.GetString("Temperature2"));
                    chart1.Series["Series1"].ChartType = SeriesChartType.FastLine;
                    chart1.Series["Series1"].Color = Color.Blue;
                }
            }

        }

        private void button3_Click(object sender, EventArgs e)
        {
            graph();
            Getweather();
            database();
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            graph();
            Getweather();
            database();
        }

        private void radioButton1_CheckedChanged(object sender, EventArgs e)
        {
            double celc = (Int32.Parse(Temperature) - 32) / (1.8);
            string Temperature2 = celc.ToString();
            textBox2.Text = Temperature2;
        }

        private void radioButton2_CheckedChanged(object sender, EventArgs e)
        {
            textBox2.Text = Temperature;
        }

        private void textBox7_TextChanged(object sender, EventArgs e)
        {

        }

        private void button4_Click(object sender, EventArgs e)
        {
            cityNumber = Int32.Parse(textBox7.Text);
            timer1.Interval = Int32.Parse(textBox8.Text);
        }

        private void optionsToolStripMenuItem_Click(object sender, EventArgs e)
        {

        }

        private void tabPage3_Click(object sender, EventArgs e)
        {

        }

        private void toolStripTextBox1_Click(object sender, EventArgs e)
        {
            Getweather();
            toolStripTextBox1.Text = "Temperature:" + Temperature + " F";
        }

        private void weatherCondition_Click(object sender, EventArgs e)
        {

        }


       }
        }

For database in localhost
 CREATE TABLE London
 (ID INT NOT NULL AUTO_INCREMENT, City VARCHAR(30) NOT NULL, Time       VARCHAR(20) NOT NULL, Temperature VARCHAR(5) NOT NULL, Temperature2 VARCHAR(5) NOT NULL,  WeatherCondition VARCHAR(30) NOT NULL, PRIMARY KEY (ID));

Can only say for Yahoo Weather JSON format. 只能说Yahoo Weather JSON格式。 The result will be in query.results.channel.item.forecast . 结果将在query.results.channel.item.forecast Check out the following example, which uses Newtonsoft.Json for JSON parsing: 请查看以下示例,该示例使用Newtonsoft.Json进行JSON解析:

dynamic jsonData = JObject.Parse(responseFromServer);
if (jsonData["query"].count == 0)
{
    throw new Exception("Failed to obtain forecast from the server.");
}

var convertFromFtoC = jsonData["query"].results.channel.units.temperature == "F";

foreach (var v in jsonData["query"].results.channel.item.forecast)
{
    var date = (DateTime)v.date;
    var tempLo = (int)v.low;
    var tempHi = (int)v.high;

    if (convertFromFtoC)
    {
        tempLo = TemperatureConverter.FahrenheitToCelcius(tempLo);
        tempHi = TemperatureConverter.FahrenheitToCelcius(tempHi);
    }

    dailyForecst.Add(new ForecastDataForTheDay { Date = date, TempLo = tempLo, TempHi = tempHi });
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM