我正在尝试为windows通用应用程序进行概念验证,调用并显示Riot API数据。id想输入一个名字,点击提交,结果显示在下面。
**问题:**分析值时遇到意外字符:h。路径",第0行,位置0
AND:此异步方法缺少"await"运算符,将同步运行。考虑使用"等待"运算符来等待非阻塞API调用,或"等待任务。运行
代码:public sealed分部类MainPage:Page{
public MainPage()
{
this.InitializeComponent();
}
private void inputButton_Click(object sender, RoutedEventArgs e)
{
GetSumInfo(nameInput.Text).Wait();
}
private void nameInput_TextChanged(object sender, TextChangedEventArgs e)
{
}
public async Task GetSumInfo(string nameInput)
{
using (var handler = new HttpClientHandler())
using (var client = new System.Net.Http.HttpClient(handler))
{
var RitoKey = "FakeKEY";
var iwantthisplayer = nameInput;
client.BaseAddress = new Uri("https://na.api.pvp.net/api/lol/na/v1.4/summoner/by-name/");
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
var CompleteUrl = ("https://na.api.pvp.net/api/lol/na/v1.4/summoner/by-name/" + iwantthisplayer + "?api_key=" + RitoKey);
//var response = await client.GetAsync(CompleteUrl);
TbOutput.Text = JsonConvert.DeserializeObject<dynamic>(CompleteUrl);
//TbOutput.Text = JsonConvert.DeserializeObject<dynamic>(jsoncall);
//TbOutput.Text = source;
}
}
private void TbOutput_SelectionChanged(object sender, RoutedEventArgs e)
{
}
}
对于这部分问题[AND:此异步方法缺少'await'运算符,将同步运行。请考虑使用'await]运算符来等待非阻塞的API调用,或'awaitTask.run]我的答案是更改代码的这一部分:
async private void inputButton_Click(object sender, RoutedEventArgs e)
{
await GetSumInfo(nameInput.Text);
}