C# 如何处理来自httpclient的数据

C# 如何处理来自httpclient的数据,c#,json,mvvm,windows-phone-8,asynchttpclient,C#,Json,Mvvm,Windows Phone 8,Asynchttpclient,我正在开发一个新的WindowsPhone8应用程序。我正在连接一个返回有效json数据的Web服务。我正在使用longlistselector显示数据。当我在GetAccountList()中使用字符串json时,这可以正常工作;但是当从DataServices类接收数据时,我得到一个错误“不能隐式地将类型'System.Threading.Tasks.Task'转换为字符串”。不知道出了什么问题。欢迎任何帮助。谢谢 DataServices.cs public async stati

我正在开发一个新的WindowsPhone8应用程序。我正在连接一个返回有效json数据的Web服务。我正在使用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);
    }
 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;
    }
公共异步静态任务GetRequest(字符串url) { HttpClient HttpClient=新HttpClient(); 等待任务。延迟(250); HttpResponseMessage response=等待httpClient.GetAsync(url); response.EnsureSuccessStatusCode(); string responseBody=wait response.Content.ReadAsStringAsync(); Debug.WriteLine(responseBody); 返回等待任务。运行(()=>ResponseBy); } AccountViewModel.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);
    }
 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;
    }
公共静态列表GetAccountList()
{
字符串json=DataService.GetRequest(url);
//字符串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 accountList=新列表();
var deserialized=JsonConvert.DeserializeObject(json);
JArray recordList=反序列化[“帐户”];
foreach(记录列表中的作业对象记录)
{
accountList.Add(新的AccountModel(记录[“name”].ToString(),记录[“id”].ToString());
}
返回帐户列表;
}
更新:我对它做了一点小小的修改,现在它就像一个魔咒。谢谢你的帮助! 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;
    }
//获取请求
公共异步静态任务GetAsync(字符串url)
{
var httpClient=新的httpClient();
var response=wait-httpClient.GetAsync(url);
string content=wait response.content.ReadAsStringAsync();
返回内容;
}
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;
    }
public异步void LoadData()
{
this.Json=await-DataService.GetAsync(url);
this.Accounts=GetAccounts(Json);
this.AccountList=GetAccountList(Accounts);
this.IsDataLoaded=true;
}
公共静态IList GetAccounts(字符串json)
{
动态上下文=JObject.Parse(json);
JArray反序列化=(JArray)JsonConvert.DeserializeObject(context.results.ToString());
IList accounts=反序列化的.ToObject();
归还账户;
}
公共静态列表GetAccountList(IList帐户)
{
List accountList=AlphaKeyGroup.CreateGroups(帐户,
System.Threading.Thread.CurrentThread.CurrentUICulture,
(AccountModels)=>{return s.Name;},true);
返回帐户列表;
}

那一行是你的问题:

return await Task.Run(() => responseBody);
你试过了吗

return responseBody;
也试试这个:

public async static List<AccountModel> GetAccountList()
{
    string json = await DataService.GetRequest(url);
    ...
}
public异步静态列表GetAccountList()
{
string json=wait DataService.GetRequest(url);
...
}

这里有几件事。首先是错误

无法将类型“System.Threading.Tasks.Task”隐式转换为字符串 此错误来自对
DataService.GetRequest(url)
的调用。此方法确实返回字符串。Tt返回一个字符串,其中T是字符串。有许多方法可以使用此方法的结果。第一个(最好的/最新的)是等待对该方法的调用

进行此更改需要将
async
键盘添加到方法中

public async static List<AccountModel> GetAccountList()
现在来看GetResult方法。使用异步/等待模式需要从方法返回任务。即使返回类型是Task,代码也应该返回T

return responseBody;

下面是一个关于从异步方法返回任务的示例。

仍然相同。它在GetAccountList()中的“string json=DataService.GetRequest(url);”上给出了错误。是的,它将在那里给出错误,原因是异步。但是请在GetReuquest方法中设置一个断点,并告诉我们“真的”异常在哪里:)