c# - 如何处理来自httpclient的数据

标签 c# json mvvm windows-phone-8 asynchttpclient

我正在开发一款新的 Windows Phone 8 应用程序。我正在连接到返回有效 json 数据的网络服务。我正在使用 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;
    }

更新:我稍微修改了一下,现在效果很好。谢谢你的帮助! 数据服务.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);
    ...
}

关于c# - 如何处理来自httpclient的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19468832/

相关文章:

javascript - JSON响应上的重复 block

c# - 使用 View 模型的ASP.net MVC演示项目

WPF MVVM 图表更改轴

c# - MVVM、ComboBox 绑定(bind)和 DatagridTemplateColumn?

c# - 无法将当前 JSON 数组(例如 [1,2,3])反序列化为类型

javascript - 使用 javascript 或 jQuery 删除多层嵌套 JSON 中的元素

c# - SELECT2 4.0 在 ASP.Net : How to get and set selected items? 中进行多选

c# - .NET Core API 中的 Twilio StatusCallBack 和 Gather POST 方法返回 HTTP 415 - 不支持的媒体类型

c# - 无法下载包含视频的子网站

c# - 如何交叉两个不同的 IEnumerable 集合