如何从 Xamarin 中的 WebAPI 获取结果值
How to get result value from WebAPI in Xamarin
public async Task<Customer> GetCustomersAsync(string id)
{
var prod = new Customer();
HttpClient client = new HttpClient();
string url = "https://xxxxxx.com/api/Customers/" + id;
client.BaseAddress = new Uri(url);
HttpResponseMessage response = await client.GetAsync("");
if (response.IsSuccessStatusCode)
{
string content = response.Content.ReadAsStringAsync().Result;
prod = JsonConvert.DeserializeObject<Customer>(content);
}
return await Task.FromResult(prod);
}
Class 客户(型号)
public class Customer
{
public string CodeRandom { get; set; }
public string NameUs { get; set; }
}
我打电话给 API 以获取结果。但是当 .Result
时我无法得到 return 结果
我是这样做的:
var infocustomer = customerRepository.GetCustomersAsync(userrating);
string nameus = infocustomer.Result.NameUs;
- 当我调试时,
nameus
自行退出。请给我任何解决方案。谢谢
而不是这个
string content = response.Content.ReadAsStringAsync().Result;
这样做
string content = await response.Content.ReadAsStringAsync();
然后
return prod;
public async Task<Customer> GetCustomersAsync(string id)
{
var prod = new Customer();
HttpClient client = new HttpClient();
string url = "https://xxxxxx.com/api/Customers/" + id;
client.BaseAddress = new Uri(url);
HttpResponseMessage response = await client.GetAsync("");
if (response.IsSuccessStatusCode)
{
string content = response.Content.ReadAsStringAsync().Result;
prod = JsonConvert.DeserializeObject<Customer>(content);
}
return await Task.FromResult(prod);
}
Class 客户(型号)
public class Customer { public string CodeRandom { get; set; } public string NameUs { get; set; } }
我打电话给 API 以获取结果。但是当 .Result
我是这样做的:
var infocustomer = customerRepository.GetCustomersAsync(userrating);
string nameus = infocustomer.Result.NameUs;
- 当我调试时,
nameus
自行退出。请给我任何解决方案。谢谢
而不是这个
string content = response.Content.ReadAsStringAsync().Result;
这样做
string content = await response.Content.ReadAsStringAsync();
然后
return prod;