不显示数据休息获取Xamarin表格



当我显示响应时,我得到的只是一个无效的结果。我认为这可能是课程,因为我正在连接到HTTP。有任何想法吗 ?谢谢 !

public class info 
    {
        public class Text
        {
            public int userId { get; set; }
            public int id { get; set; }
            public string title { get; set; }
            public string body { get; set; }
        }

这是呼叫:

Device.BeginInvokeOnMainThread(async () =>
            {
                RestClient client = new RestClient();
                var apiresult = await client.Get<info>("https://jsonplaceholder.typicode.com/posts");

                if (apiresult != null)
                {
                    labelchange.Text = apiresult.Text.title;
                }
            });

和实际休息

public class RestClient
    {
        public async Task <T> Get <T>(string url)
        {
            try
            {
                HttpClient client = new HttpClient();
                var response = await client.GetAsync(url);
                if (response.StatusCode == System.Net.HttpStatusCode.OK)
                {
                    var jsonstring = await response.Content.ReadAsStringAsync();
                    return Newtonsoft.Json.JsonConvert.DeserializeObject<T>(jsonstring);
                }
            }
            catch
            {
            }
            return default(T);
        }
    }

您传递给RESTClient的对象类型不正确。从https://jsonplaceholder.typicode.com/posts返回的类型是 List<Text>

因此,您应该更改以下行:

var apiresult = await client.Get<List<Text>>("https://jsonplaceholder.typicode.com/posts");

要在结果中访问成员,只是循环循环结果:

foreach (var info in apiresult)
{
    // Do something with info. e.g. info.userId, info.id...
}

相关内容

  • 没有找到相关文章

最新更新