我目前正在使用Xamarin为手机创建一个多平台应用程序。应用程序的想法基本上是一个口袋妖怪百科全书,它利用了PokeAPi (https://pokeapi.co/),也使用了下面的包装库(https://gitlab.com/PoroCYon/PokeApi.NET)。目前,我希望它的位置,如果用户在搜索栏键入一个错误的精灵宝可梦,它将返回一个警告错误给用户。然而,每次我测试它并输入一个无效的pokemon时,应用程序就会停止,Visual Studio/Xamarin会通知我一个HTTP404错误。我该怎么做呢?
我尝试使用比较语句,如果应用程序接口调用没有找到精灵宝可梦的名字,它应该弹出一个警告,但VS/Xamarin将停止运行应用程序并显示Http404异常。在这一点上我真的不知道该去哪里。
“”“
async Task PullData()
{
LoadingIcon.IsRunning = true;
string newPokemon = PokemonFind.Text;
Pokemon p = await DataFetcher.GetNamedApiObject<Pokemon>(newPokemon);
string PokemonName = p.Name;
int PokemonHeight = p.Height;
int PokemonWeight = p.Mass;
int PokemonXp = p.BaseExperience;
int PokemonOrder = p.Order;
OrderLabel.Text = "#" + PokemonOrder;
NameLabel.Text = "Name: " + PokemonName;
HeightWeightLabel.Text = "Height/Weight: " + PokemonHeight.ToString() +" dm " + "/" + PokemonWeight.ToString() + " hg";
ExpLabel.Text = "Experience on defeat: " + PokemonXp.ToString() + "XP";
LoadingIcon.IsRunning = false;
}
“”“
我预计它会显示一条警告消息,而不是VS/Xamarin停止程序并抛出HTTP404异常。
发布于 2019-05-03 18:03:32
将您的调用包装在try/catch块中
try
{
async Task PullData()
}
catch(HttpRequestException ex)
{
//Shows an alert error to the user
}
https://stackoverflow.com/questions/55966189
复制相似问题