I want to print the weather of a certain place coming from windy in Winform using the paint forecast API, but I see that there is an error in the url or apikey (
Cannot GET /api/point-forecast/v2) I am sharing my code, urgent help. public partial class Frmsettings : Form
{
private Timer dataFetchTimer;
private static readonly HttpClient client = new HttpClient();
public Frmsettings()
{
InitializeComponent();
// Timer ayarlarını yapıyoruz
dataFetchTimer = new Timer();
dataFetchTimer.Interval = 60000; // 60 saniyede bir veri çekme (1000ms * 60)
dataFetchTimer.Tick += DataFetchTimer_Tick; // Timer tetiklendiğinde bu fonksiyon çalışacak
MessageBox.Show("Timer interval set to: " + dataFetchTimer.Interval); // Interval'in doğru ayarlandığını kontrol edin
dataFetchTimer.Start(); // Timer'ı başlat
}
// Timer her tetiklendiğinde API'den veri çekme
private async void DataFetchTimer_Tick(object sender, EventArgs e)
{
MessageBox.Show("Timer Triggered!"); // Bu mesaj görünüyor mu?
await FetchWeatherData(); // Veriyi çekme işlemini başlat
}
private async Task FetchWeatherData()
{
string apiKey = "HcuoyBroUc8HeHz4ITN64JgnTAgHSCbY"; // Windy API anahtarınız
double latitude = 40.091; // Enlem
double longitude = 33.409; // Boylam
// API URL'sini yazdırma
string url = $"https://api.windy.com/api/point-forecast/v2?lat={latitude}&lon={longitude}&model=gfs¶meters=wind&levels=1000,500,250&key={apiKey}";
// Mesajları UI thread'de göstermek
this.Invoke((MethodInvoker)delegate
{
MessageBox.Show("API URL: " + url); // URL'yi yazdırın
});
try
{
// API'ye istek gönderme
HttpResponseMessage response = await client.GetAsync(url);
// Yanıtın durumu
if (!response.IsSuccessStatusCode)
{
this.Invoke((MethodInvoker)delegate
{
MessageBox.Show($"Error: {response.StatusCode} - {response.ReasonPhrase}");
});
return;
}
string responseBody = await response.Content.ReadAsStringAsync();
this.Invoke((MethodInvoker)delegate
{
MessageBox.Show("API Response Body: " + responseBody); // Yanıtı yazdır
});
// JSON verisini işleme (örneğin, rüzgar hızı ve yönü)
var weatherData = JsonConvert.DeserializeObject(responseBody);
this.Invoke((MethodInvoker)delegate
{
MessageBox.Show($"Deserialized Data: {weatherData}"); // Deserialize edilmiş veriyi yazdır
});
if (weatherData != null && weatherData.wind != null && weatherData.wind.Length > 0)
{
this.Invoke((MethodInvoker)delegate
{
MessageBox.Show($"Wind Speed: {weatherData.wind[0].speed} m/s");
MessageBox.Show($"Wind Direction: {weatherData.wind[0].direction} degrees");
});
}
else
{
this.Invoke((MethodInvoker)delegate
{
MessageBox.Show("Wind data not available");
});
}
}
catch (HttpRequestException ex)
{
this.Invoke((MethodInvoker)delegate
{
MessageBox.Show($"Error: {ex.Message}");
});
}
}
// JSON cevabını işlemek için sınıf
public class WeatherResponse
{
public WindData[] wind { get; set; }
}
public class WindData
{
public double speed { get; set; }
public double direction { get; set; }
}
//Kalecik enlem boylam
//40.080, 33.404
private void Frmsettings_Load(object sender, EventArgs e)
{
// Bu mesajı görüp görmediğinizi kontrol etmek için kullanabilirsiniz
MessageBox.Show("Form Loaded!"); // Bu mesajı görmelisiniz
dataFetchTimer.Start();
}