I am building an Android app with Xamarin that grabs data from Riot Games API and displays to the user. I want the HTTP request to be made in the background and update my UI, once it's done. I tried using ThreadPool.QueueUserWorkItem() but it executes the code below immediately and I want it to wait until it has grabbed the data. This is the code on my portable class library.
public void GetSummonerInformation()
{
try
{
HttpResponseMessage response = httpClient.GetAsync(url).Result;
response.EnsureSuccessStatusCode();
string result = response.Content.ReadAsStringAsync().Result;
var data = JsonConvert.DeserializeObject<Dictionary<string, App2.Models.SummonerDto>>(result);
var name1 = data.First().Value.name;
var id = data.First().Value.id;
var profileIconId1 = data.First().Value.profileIconId;
var revisionDate1 = data.First().Value.revisionDate;
sumId = id;
sumName1 = name1;
sumProfileIconId = profileIconId1;
sumRevisionDate = revisionDate1;
System.Diagnostics.Debug.WriteLine("{0} this is the {1}", data.First().Value.name, data.First().Value.profileIconId);
}catch(Exception ex)
{ System.Diagnostics.Debug.WriteLine(ex.Message); }
and this is my code on my Android mainactivity.cs
button2nd.Click += delegate
{
//Runs the http request on a background thread so the application doesnt hang.Need to make the code after that to wait for the request to end and then exexute
//because it gives me a blank icon and i need to press the button again.
ThreadPool.QueueUserWorkItem(o => mclass.GetSummonerInformation());
System.Diagnostics.Debug.WriteLine(MyClass.sumProfileIconId);
iconUrl = string.Format("http://ddragon.leagueoflegends.com/cdn/6.24.1/img/profileicon/" + MyClass.sumProfileIconId + ".png");
DisplaySummonerIcon();
DisplaySummonerInformation();
}
Thanks!
You're using asynchronous APIs (
HttpClient
), so you should useawait
:invoked as such:
For more information about
async
andawait
, see my blog.