[英]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.