Example #1
0
 private static Response5Days MakeRequest5Days(string requestUrl)
 {
     try
     {
         HttpWebRequest request = WebRequest.Create(requestUrl) as HttpWebRequest;
         using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
         {
             if (response.StatusCode != HttpStatusCode.OK)
             {
                 throw new Exception(String.Format(
                                         "Server error (HTTP {0}: {1}).",
                                         response.StatusCode,
                                         response.StatusDescription));
             }
             DataContractJsonSerializer jsonSerializer = new DataContractJsonSerializer(typeof(Response5Days));
             object        objResponse = jsonSerializer.ReadObject(response.GetResponseStream());
             Response5Days jsonResponse
                 = objResponse as Response5Days;
             return(jsonResponse);
         }
     }
     catch (Exception e)
     {
         Console.WriteLine(e.Message);
         return(null);
     }
 }
Example #2
0
        private void forecastBtn_Click(object sender, EventArgs e)
        {
            string  line;
            Boolean bFound = false;

            string text = "\"" + txtCity.Text + "\"";
            string UpperFirstLetterCity = FormatWordsWithFirstCapital(text.ToLower());

            System.IO.StreamReader file =
                new System.IO.StreamReader("city.list.us.json");

            while ((line = file.ReadLine()) != null)
            {
                if (line.Contains(UpperFirstLetterCity))
                {
                    bFound = true;

                    try
                    {
                        string        weatherRequest  = CreateRequest5Days(txtCity.Text);
                        Response5Days weatherResponse = MakeRequest5Days(weatherRequest);
                        ProcessResponse5Days(weatherResponse);
                    }
                    catch (Exception excep)
                    {
                        Console.WriteLine(excep.Message);
                        Console.Read();
                    }
                }
            }

            if (bFound == false)
            {
                MessageBox.Show("Either entering City is not available or spelling is incorrect", "Warning Message");
            }
        }
Example #3
0
        private void ProcessResponse5Days(Response5Days weatherResponse)
        {
            string[]  arrTemp  = new string[40];
            decimal[] darrTemp = new decimal[40];

            minTemp = 300M;
            maxTemp = -300M;

            lblTemperature0.Text = weatherResponse.List[0].main1.Temp1.ToString();
            lblTemperature1.Text = weatherResponse.List[1].main1.Temp1.ToString();
            lblTemperature2.Text = weatherResponse.List[2].main1.Temp1.ToString();
            lblTemperature3.Text = weatherResponse.List[3].main1.Temp1.ToString();
            lblTemperature4.Text = weatherResponse.List[4].main1.Temp1.ToString();
            lblTemperature5.Text = weatherResponse.List[5].main1.Temp1.ToString();
            lblTemperature6.Text = weatherResponse.List[6].main1.Temp1.ToString();
            lblTemperature7.Text = weatherResponse.List[7].main1.Temp1.ToString();

            for (int i = 0; i < weatherResponse.List.Length; i++)
            {
                arrTemp[i]  = weatherResponse.List[i].main1.Temp1.ToString();
                darrTemp[i] = Decimal.Parse(arrTemp[i]);
            }

            System.DateTime dt       = dtp.Value;
            int             diffDays = (int)Math.Round((dtp.Value - DateTime.Now).TotalDays);

            int offset = (diffDays - 1) * 8;

            if (rbCelsius.Checked)
            {
                for (int j = 0; j < 8; j++)
                {
                    darrTemp[offset + j] = darrTemp[offset + j] - 273.15M;
                    temp[j] = darrTemp[offset + j];

                    if (minTemp > temp[j])
                    {
                        minTemp = temp[j];
                    }
                    if (maxTemp < temp[j])
                    {
                        maxTemp = temp[j];
                    }
                }

                lblTemperature0.Text = darrTemp[offset].ToString("N1") + "°C";
                lblTemperature1.Text = darrTemp[offset + 1].ToString("N1") + "°C";
                lblTemperature2.Text = darrTemp[offset + 2].ToString("N1") + "°C";
                lblTemperature3.Text = darrTemp[offset + 3].ToString("N1") + "°C";
                lblTemperature4.Text = darrTemp[offset + 4].ToString("N1") + "°C";
                lblTemperature5.Text = darrTemp[offset + 5].ToString("N1") + "°C";
                lblTemperature6.Text = darrTemp[offset + 6].ToString("N1") + "°C";
                lblTemperature7.Text = darrTemp[offset + 7].ToString("N1") + "°C";
                lblMinMaxTemp.Text   = minTemp.ToString("N1") + "°C" + " / " + maxTemp.ToString("N1") + "°C";
            }
            else
            {
                for (int j = 0; j < 8; j++)
                {
                    darrTemp[offset + j] = (darrTemp[offset + j] * 9.0M / 5.0M) - 459.67M;
                    temp[j] = darrTemp[offset + j];

                    if (minTemp > temp[j])
                    {
                        minTemp = temp[j];
                    }
                    if (maxTemp < temp[j])
                    {
                        maxTemp = temp[j];
                    }
                }

                lblTemperature0.Text = darrTemp[offset].ToString("N1") + "°F";
                lblTemperature1.Text = darrTemp[offset + 1].ToString("N1") + "°F";
                lblTemperature2.Text = darrTemp[offset + 2].ToString("N1") + "°F";
                lblTemperature3.Text = darrTemp[offset + 3].ToString("N1") + "°F";
                lblTemperature4.Text = darrTemp[offset + 4].ToString("N1") + "°F";
                lblTemperature5.Text = darrTemp[offset + 5].ToString("N1") + "°F";
                lblTemperature6.Text = darrTemp[offset + 6].ToString("N1") + "°F";
                lblTemperature7.Text = darrTemp[offset + 7].ToString("N1") + "°F";
                lblMinMaxTemp.Text   = minTemp.ToString("N1") + "°F" + " / " + maxTemp.ToString("N1") + "°F";
            }
        }