0

我正在使用 Xamarin 构建一个 Android 应用程序,该应用程序从 Riot Games API 获取数据并显示给用户。我希望在后台发出 HTTP 请求并在完成后更新我的 UI。我尝试使用 ThreadPool.QueueUserWorkItem() 但它立即执行下面的代码,我希望它等到它抓取数据。这是我的可移植类库上的代码。

 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); }

这是我在 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();
}

谢谢!

4

2 回答 2

0

您正在使用异步 API ( HttpClient),因此您应该使用await

public async Task GetSummonerInformationAsync()
{
  try
  {
    HttpResponseMessage response = await httpClient.GetAsync(url);
    response.EnsureSuccessStatusCode();
    string result = await response.Content.ReadAsStringAsync();
    var data = JsonConvert.DeserializeObject<Dictionary<string, App2.Models.SummonerDto>>(result);
    ...
    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);
  }
}

这样调用:

button2nd.Click += async (_, __) =>
{
  await mclass.GetSummonerInformationAsync();
  System.Diagnostics.Debug.WriteLine(MyClass.sumProfileIconId);
  iconUrl = string.Format("http://ddragon.leagueoflegends.com/cdn/6.24.1/img/profileicon/" + MyClass.sumProfileIconId + ".png");
  DisplaySummonerIcon();
  DisplaySummonerInformation();
};

有关asyncand的更多信息await请参阅我的博客

于 2016-12-17T23:08:18.750 回答
0

改变:

ThreadPool.QueueUserWorkItem(o => mclass.GetSummonerInformation());

和:

await Task.Run(() => mclass.GetSummonerInformation());

并在 'delegate' 之前添加 'async' 关键字;

于 2016-12-14T20:37:45.060 回答