Best way to check internet connection

hello,

My app is populate from a json data Online;
As xamarin essential not give real connection status i prefer to check online.

protected override async void OnAppearing()
        {
            if (CheckForInternetConnection() == true)
            {
                isinternet.Text = "Internet ok";
// download json
            }
            else
            {
                isinternet.Text = "Internet down";
//use old json
            }

          // await Navigation.PushAsync(new MainPage());
        }
 public static bool CheckForInternetConnection()
        {
            
            try
            {
                using (var client = new MyWebClient(5000))
                    
                using (client.OpenRead("http://google.com/generate_204"))
                    return true;
// download json data
            }
            catch
            {
                return false;
//use old json
            }
        }

Is it good solution? any way to make it async and not freeze app? httpclient is fastest?

I choose to download jason file then can use old one if no connection.

webclient.downloadfile is only for binary no? i need to use webclient.DownloadString and create the json file i think


Thanks

3 Replies 1 reply marked as answer

SS Suganya Sethuraman Syncfusion Team September 2, 2020 06:18 AM UTC

Hi Milka,

Greetings from Syncfusion.

We have analyzed your query “Best way to check internet connection”. Please refer the following link to achieve your requirement,

Link: https://docs.microsoft.com/en-us/xamarin/essentials/connectivity?tabs=android

Please let us know if you have any concerns.

Regards,
Suganya Sethuraman 


Marked as answer

SK Sheetal Kamble December 9, 2020 11:13 AM UTC

exactly what I was looking for. Thanks!


SS Suganya Sethuraman Syncfusion Team December 10, 2020 04:28 AM UTC

Hi Sheetal,

Thanks for the update.

Regards,
Suganya Sethuraman
 


Loader.
Up arrow icon