mardi 9 avril 2019

How can i get weather of the selected cities from checkbox list using the weather API

i am getting the weather of the cities from weather map API i want to extract the weather data of the selected cities by using checkbox values. i am unable to pass the selected cities value to the api.

public ActionResult Index(string cities) { ClimateModel openWeatherMap = FillCity();

        if (cities != null)
        {

            string apiKey = "e2c267b5316573acf93d5ee47a9d05e0";
            HttpWebRequest apiRequest = WebRequest.Create("http://api.openweathermap.org/data/2.5/weather?id=" + cities + "&appid=" + apiKey + "&units=metric") as HttpWebRequest;

            string apiResponse = "";
            using (HttpWebResponse response = apiRequest.GetResponse() as HttpWebResponse)
            {
                StreamReader reader = new StreamReader(response.GetResponseStream());
                apiResponse = reader.ReadToEnd();
            }

            //var rootObject = JsonConvert.DeserializeObject<ResponseWeather>(apiResponse);
            var rootObject = JsonConvert.DeserializeObject<ResponseWeather>(apiResponse);

            StringBuilder sb = new StringBuilder();
            sb.Append("<table><tr><th>Weather Description</th></tr>");
            sb.Append("<tr><td>City:</td><td>" + rootObject.name + "</td></tr>");
            sb.Append("<tr><td>Country:</td><td>" + rootObject.sys.country + "</td></tr>");
            sb.Append("<tr><td>Country Sun Rise:</td><td>" + rootObject.sys.sunrise + "</td></tr>");
            sb.Append("<tr><td>Country Sun Sete:</td><td>" + rootObject.sys.sunset + "</td></tr>");
            //sb.Append("<tr><td>Wind:</td><td>" + rootObject.wind.speed + " Km/h</td></tr>");
            sb.Append("<tr><td>Current Temperature:</td><td>" + rootObject.main.temp + " °C</td></tr>"
            sb.Append("<tr><td>Weather:</td><td>" + rootObject.weather[0].description + "</td></tr>");
            sb.Append("</table>");
            openWeatherMap.apiResponce = sb.ToString();
        }
        else
        {
            if (Request.Form["submit"] != null)
            {
                TempData["SelectOption"] = -1;
            }
        }
        return View(openWeatherMap);
    }




Aucun commentaire:

Enregistrer un commentaire