Skip to content

Commit

Permalink
Fixed crash when launching the app without internet connection (#605)
Browse files Browse the repository at this point in the history
fixes #605
  • Loading branch information
lpeyr committed Aug 23, 2024
1 parent 28d0990 commit e8cb5c0
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 6 deletions.
14 changes: 10 additions & 4 deletions InternetTest/InternetTest/Classes/Global.cs
Original file line number Diff line number Diff line change
Expand Up @@ -287,10 +287,16 @@ public static bool IsUrlValid(string url)

public async static Task<IPInfo?> GetIPInfoAsync(string ip)
{
HttpClient httpClient = new();
string result = await httpClient.GetStringAsync($"http://ip-api.com/json/{ip}");

return JsonSerializer.Deserialize<IPInfo>(result);
try
{
HttpClient httpClient = new();
string result = await httpClient.GetStringAsync($"http://ip-api.com/json/{ip}");
return JsonSerializer.Deserialize<IPInfo>(result);
}
catch
{
return null;
}
}

public static bool IsIpValid(string ip)
Expand Down
4 changes: 2 additions & 2 deletions InternetTest/InternetTest/Pages/HomePage.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ internal async void InitUI()
LoadNetworkCard();

// Load "My IP" section
ip = (await Global.GetIPInfoAsync("")).Query ?? "";
ip = (await Global.GetIPInfoAsync(""))?.Query ?? "";
}

private async void RefreshStatusBtn_Click(object sender, System.Windows.RoutedEventArgs e)
Expand Down Expand Up @@ -127,7 +127,7 @@ private void RefreshNetworkBtn_Click(object sender, System.Windows.RoutedEventAr
string ip = "";
private async void RefreshMyIpBtn_Click(object sender, System.Windows.RoutedEventArgs e)
{
ip = (await Global.GetIPInfoAsync("")).Query ?? "";
ip = (await Global.GetIPInfoAsync(""))?.Query ?? "";
}

private void MyIpBorder_MouseEnter(object sender, System.Windows.Input.MouseEventArgs e)
Expand Down

0 comments on commit e8cb5c0

Please sign in to comment.