Created
May 29, 2018 08:27
-
-
Save jfversluis/2c65e8ec62eaf0fb1d37dba040a1d57e to your computer and use it in GitHub Desktop.
Retrieving data
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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