Skip to content

Instantly share code, notes, and snippets.

@jfversluis
Created May 29, 2018 08:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jfversluis/2c65e8ec62eaf0fb1d37dba040a1d57e to your computer and use it in GitHub Desktop.
Save jfversluis/2c65e8ec62eaf0fb1d37dba040a1d57e to your computer and use it in GitHub Desktop.
Retrieving data
public MainPage()
{
InitializeComponent();
_customHttpClientHandler = new EtagHttpClientHandler();
_customHttpClient = new HttpClient(_customHttpClientHandler)
{
BaseAddress = new Uri(ApiBaseUrl)
};
RefreshDataCommand = new Command(async () => await GetSuperheroes());
BindingContext = this;
RefreshDataCommand.Execute(null);
}
public async Task GetSuperheroes()
{
IsLoading = true;
try
{
var superheroesJson = await _customHttpClient.GetStringAsync("superhero");
var superheroes = JsonConvert.DeserializeObject<Superhero[]>(superheroesJson);
Superheroes.Clear();
foreach (var hero in superheroes)
Superheroes.Add(hero);
}
catch (HttpRequestException ex) when (ex.Message.Contains("304"))
{
// Intentionally left blank, 304 response is fine
}
finally
{
IsLoading = false;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment