简体   繁体   English

没有互联网连接的iOS应用崩溃

[英]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. 我正在使用async方法来检索JSON数据,数据成功发送,但是当我关闭设备中的Internet时,应用程序不幸地停止了运行。 How can I handle this exception, or how can I handle internet Connectivity in Xamarin.Forms. 如何处理此异常,或者如何处理Xamarin.Forms中的Internet连接。

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. 使用简单的try / catch结构,这也有助于解决可能发生的任何其他错误。 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. 您不需要自己呼叫OnAppearing 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;
    });
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 没有互联网时Windows应用商店应用崩溃 - Windows store app crashes when no internet 应用程序崩溃 IOS 在 PCL 中启动 Xamarin - App Crashes On IOS Start Up In PCL Xamarin Xamarin iOS:启动计时器无一例外使应用程序崩溃 - Xamarin iOS: Starting timer crashes app with no exception Unity iOS 应用程序因不明原因而崩溃 - Unity iOS app crashes for unclear reasons Silverlight应用延迟了没有互联网连接的计算机上的加载 - Silverlight app delayed loading on computer without internet connection 您如何确定WinForms应用程序是否可以使用Internet连接? - How do you determine if an Internet connection is available for your WinForms App? wp8 app互联网连接检查并防止未处理的关闭 - wp8 app internet connection check and prevent unhandled shut down 在控制台应用程序C#中检测Internet连接/断开连接 - detect Internet Connection/Disconnection in Console App C# 当我尝试加载数据并且应用程序没有互联网时,如何避免该应用程序崩溃? - How can I avoid that the app crashes when I try to load data and the app has no internet? SQL Server Compact-应用程序崩溃时是否断开了数据库连接? - SQL Server Compact - Is the DB connection disposed when the app crashes?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM