[英]API GET method doesn't store data in the property in Xamarin Forms
Data is being retrieve from the API succesfully, as I can see it here, response and then goes to the jsonstring , but never gets to the CantGet variable数据正在从 API 成功检索,正如我在这里看到的那样,响应然后转到jsonstring ,但从未到达CantGet变量
I need it to be store in my property so I can use the value.我需要将它存储在我的财产中,以便我可以使用该值。
This is my API return: [{"CantPremio":"70"}这是我的 API 返回:[{"CantPremio":"70"}
Then this is my property:然后这是我的财产:
using System;
using System.Collections.Generic;
using System.Text;
namespace ServLottery
{
public class GetCantPremio
{
public long CantPremio { get; set; }
}
}
This is the Get task这是获取任务
using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Security.Permissions;
using System.Text;
using System.Threading.Tasks;
namespace ServLottery
{
public class RestClient
{
HttpClient client = new HttpClient();
public async Task<T> Get<T>(string URL)
{
try
{
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);
}
}
}
Finally this is the call:最后是调用:
private async void GetCantDisponible()
{
try
{
RestClient client = new RestClient();
var CantGet = await client.Get<GetCantPremio>("https://servicentroapi.azurewebsites.net/api/GetNumber");
if (CantGet != null)
{
PremiosCantLocal = CantGet.CantPremio.ToString();
}
}
catch (Exception ex)
{
throw ex;
}
}
The api you are accessing is returning an array.您正在访问的 api 正在返回一个数组。 So you must deserialize not a simple object but a list.
所以你必须反序列化的不是一个简单的 object 而是一个列表。
return Newtonsoft.Json.JsonConvert.DeserializeObject<List<T>>(jsonstring);
Replace the line that deserializes with this one.用这一行替换反序列化的行。 Should solve the problem
应该解决问题
Like kelvin said, set the List<T>
for the json array.就像 kelvin 所说,为 json 阵列设置
List<T>
。 And then foreach the collection to get the CantPremio
.然后 foreach 集合以获取
CantPremio
。
RestClient:休息客户端:
public class RestClient
{
HttpClient client = new HttpClient();
public async Task<List<T>> Get<T>(string URL)
{
try
{
var response = await client.GetAsync(URL);
if (response.StatusCode == System.Net.HttpStatusCode.OK)
{
var jsonstring = await response.Content.ReadAsStringAsync();
var s = Newtonsoft.Json.JsonConvert.DeserializeObject<List<T>>(jsonstring);
return s;
}
}
catch
{
}
return default(List<T>);
}
}
GetCantDisponible: GetCantDisponible:
private async void GetCantDisponible()
{
try
{
RestClient client = new RestClient();
var CantGet = await client.Get<GetCantPremio>("https://servicentroapi.azurewebsites.net/api/GetNumber");
if (CantGet != null)
{
foreach (var item in CantGet)
{
var PremiosCantLocal = item.CantPremio.ToString();
}
}
}
catch (Exception ex)
{
throw ex;
}
}
Screenshot:截屏:
As mentioned, your API is returning an array but you're trying to deserialize it to a single instance.如前所述,您的 API 正在返回一个数组,但您正试图将其反序列化为单个实例。 I'd suggest changing the call site of your client to pass a list for the type parameter:
我建议更改客户的呼叫站点以传递类型参数的列表:
List<GetCantPremio> CantGet = await client.Get<List<GetCantPremio>>("https://servicentroapi.azurewebsites.net/api/GetNumber");
Note that CantGet
is now a List.请注意,
CantGet
现在是一个列表。 If you are only looking for one object you could just add on a FirstOrDefault()
:如果你只是在寻找一个 object 你可以添加一个
FirstOrDefault()
:
GetCantPremio CantGet = await client.Get<List<GetCantPremio>>("https://servicentroapi.azurewebsites.net/api/GetNumber")?.FirstOrDefault();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.