[英]How to handle data from httpclient
我正在开发一个新的Windows Phone 8应用程序。 我正在连接到一个返回有效json数据的webservice。 我正在使用longlistselector来显示数据。 当我在GetAccountList()中使用字符串json时,这很好用; 但是当从DataServices类接收数据时,我收到错误“无法隐式转换类型'System.Threading.Tasks.Task'到字符串”。 不知道出了什么问题。 欢迎任何帮助。 谢谢!
DataServices.cs
public async static Task<string> GetRequest(string url)
{
HttpClient httpClient = new HttpClient();
await Task.Delay(250);
HttpResponseMessage response = await httpClient.GetAsync(url);
response.EnsureSuccessStatusCode();
string responseBody = await response.Content.ReadAsStringAsync();
Debug.WriteLine(responseBody);
return await Task.Run(() => responseBody);
}
AccountViewModel.cs
public static List<AccountModel> GetAccountList()
{
string json = DataService.GetRequest(url);
//string json = @"{'accounts': [{'id': 1,'created': '2013-10-03T16:17:13+0200','name': 'account1 - test'},{'id': 2,'created': '2013-10-03T16:18:08+0200','name': 'account2'},{'id': 3,'created': '2013-10-04T13:23:23+0200','name': 'account3'}]}";
List<AccountModel> accountList = new List<AccountModel>();
var deserialized = JsonConvert.DeserializeObject<IDictionary<string, JArray>>(json);
JArray recordList = deserialized["accounts"];
foreach (JObject record in recordList)
{
accountList.Add(new AccountModel(record["name"].ToString(), record["id"].ToString()));
}
return accountList;
}
更新:我稍微改变它,现在就像一个魅力。 谢谢你的帮助! DataServices.cs
//GET REQUEST
public async static Task<string> GetAsync(string url)
{
var httpClient = new HttpClient();
var response = await httpClient.GetAsync(url);
string content = await response.Content.ReadAsStringAsync();
return content;
}
AccountViewModel.cs
public async void LoadData()
{
this.Json = await DataService.GetAsync(url);
this.Accounts = GetAccounts(Json);
this.AccountList = GetAccountList(Accounts);
this.IsDataLoaded = true;
}
public static IList<AccountModel> GetAccounts(string json)
{
dynamic context = JObject.Parse(json);
JArray deserialized = (JArray)JsonConvert.DeserializeObject(context.results.ToString());
IList<AccountModel> accounts = deserialized.ToObject<IList<AccountModel>>();
return accounts;
}
public static List<AlphaKeyGroup<AccountModel>> GetAccountList(IList<AccountModel> Accounts)
{
List<AlphaKeyGroup<AccountModel>> accountList = AlphaKeyGroup<AccountModel>.CreateGroups(Accounts,
System.Threading.Thread.CurrentThread.CurrentUICulture,
(AccountModel s) => { return s.Name; }, true);
return accountList;
}
那条线是你的问题:
return await Task.Run(() => responseBody);
你试过吗? :
return responseBody;
试试这个:
public async static List<AccountModel> GetAccountList()
{
string json = await DataService.GetRequest(url);
...
}
这里有一些事情。 首先是错误
无法将类型'System.Threading.Tasks.Task'隐式转换为字符串此错误来自对
DataService.GetRequest(url)
的调用。 此方法确实返回一个字符串。 Tt返回一个任务 ,其中T是一个字符串。 有许多方法可以使用此方法的结果。 第一个(最好的/最新的)是等待对方法的调用。
string json = await DataService.GetResult(url);
进行此更改需要您将async
键盘添加到方法中
public async static List<AccountModel> GetAccountList()
这是新的异步/等待模式。 添加这些单词告诉编译器方法cal是异步的。 它允许您进行异步调用,但编写代码就像它是同步的一样。 调用该方法的其他方法是直接使用Task对象。
// First is to use the Result property of the Task
// This is not recommended as it makes the call synchronous, and ties up the UI thread
string json = DataService.GetResult(url).Result;
// Next is to continue work after the Task completes.
DataService.GetResult(url).ContinueWith(t =>
{
string json = t.Result;
// other code here.
};
现在为GetResult方法。 使用async / await模式要求您从方法返回Task。 即使返回类型是Task,您的代码也应该返回T.所以,正如Krekkon所提到的,您应该将返回行更改为
return responseBody;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.