我正在使用新的Windows Phone 8应用。我正在连接到返回有效json数据的webservice。我使用longlistselector来显示数据。当我在GetAccountList()中使用字符串json时,这很好用;但是当从DataServices类接收数据时,我得到了错误"无法隐式地将类型&System; Thhreading.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;
}
答案 0 :(得分:1)
那条线是你的问题:
return await Task.Run(() => responseBody);
你试过那个吗? :
return responseBody;
试试这个:
public async static List<AccountModel> GetAccountList()
{
string json = await DataService.GetRequest(url);
...
}
答案 1 :(得分:0)
这里有一些事情。首先是错误
无法将类型'System.Threading.Tasks.Task'隐式转换为字符串 此错误来自对
DataService.GetRequest(url)
的调用。此方法确实返回一个字符串。 Tt返回Task,其中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;
以下是关于从异步方法返回任务的great article。