简体   繁体   中英

App crashes on iOS with no internet connection

I am using async method to retrieve JSON data , the data comes successfully but when I turn off the internet in my device, the App stops unfortunately. How can I handle this exception, or how can I handle internet Connectivity in Xamarin.Forms.

public partial class MainActivity : ContentPage
    {

        public ObservableCollection<Adverts> Zoos { get; set; }

        public int Count = 0;
        public short Counter = 0;
        public int SlidePosition = 0;
        string heightList;
        int heightRowsList = 90;

        private const string Url = "http://eamobiledirectory.com/cooperp/Mobile/Mobileapi.aspx?Action=Featured&Country=Uganda";
        private const string BaseImageUrl = "http://eamobiledirectory.com/cooperp/Images/app_images/";
        private HttpClient _client = new HttpClient();
        public ObservableCollection<Adverts> adverts;

        public MainActivity()
        {
            InitializeComponent();



            TrendingShows();

            if (CrossConnectivity.Current.IsConnected)
            {

                OnAppearing();
            }


            else
            {

                App.Current.MainPage.DisplayAlert("Alert ", "No internet Connection Please", "OK");
            }






        }
    protected override async void OnAppearing()
            {

                var content = await _client.GetStringAsync(Url);
                var adv = JsonConvert.DeserializeObject<List<Adverts>>(content);

                adverts = new ObservableCollection<Adverts>(adv);



                AdvertsCarousel.ItemsSource = adverts;
                // attaching auto sliding on to carouselView 
                Device.StartTimer(TimeSpan.FromSeconds(18), () =>
                {
                    SlidePosition++;
                    if (SlidePosition == adverts.Count)
                        SlidePosition = 0;
                    AdvertsCarousel.Position = SlidePosition;
                    return true;
                });



            }

i tried this but it seems not working , How can i handle this.

Either use a simple try/catch construction, this also helps for any other errors that might occur. Or look into the Connectivity Plugin . This has some methods, properties and events to determine what the state of your connection is and you can handle your content accordingly.

Edit

As a follow up to your edited question; you do not need to call OnAppearing yourself. Either abstract your code to some kind of method that refreshes the data if you need to, or find another method that is better suitable.

You should also move your check closer to the point where you are fetching your data. For instance, do it more like this:

protected override async void OnAppearing()
{
    adverts = new List<Adverts>();

    if (CrossConnectivity.Current.IsConnected)
    {
        var content = await _client.GetStringAsync(Url);
        var adv = JsonConvert.DeserializeObject<List<Adverts>>(content);

        adverts = new ObservableCollection<Adverts>(adv);
    }
    else
    {
        // TODO: Either show cached adverts here, or hide your carrousel or whatever you want to do when there is no connection
    }

    AdvertsCarousel.ItemsSource = adverts;

    // attaching auto sliding on to carouselView 
    Device.StartTimer(TimeSpan.FromSeconds(18), () =>
    {
        SlidePosition++;
        if (SlidePosition == adverts.Count)
            SlidePosition = 0;
        AdvertsCarousel.Position = SlidePosition;
        return true;
    });
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM