![](/img/trans.png)
[英]Rest API GET Method using Visual Studio 2015 and Xamarin
[英]xamarin form not calling web api using visual studio 2015
我想通過Xamarin表單調用web api(在我的本地視覺工作室),但它不會調用我的web api。 我正在使用visual studio android模擬器。
我正在使用visual studio 2015 update 3,這是我的代碼
public class DataService
{
HttpClient client = new HttpClient();
string apiPaht = "http://10.0.2.2:19367/api/";
//string apiPaht = "http://localhost:19367/api/";
public async Task<List<CustomerApp>> GetTodoItemsAsync()
{
var response = await client.GetStringAsync(apiPaht+ "APICustAccount/getTestData");
var todoItems = JsonConvert.DeserializeObject<List<CustomerApp>>(response);
return todoItems;
}
}
我的xamal.cs上的代碼
async void RefreshData()
{
List<CustomerApp> listCust = await _dService.GetTodoItemsAsync();
todoList.ItemsSource = listCust;
string aa = "";
}
protected void btn_click(object sender, EventArgs e)
{
RefreshData();
}
嗨請嘗試下面的代碼,如果您有任何疑問,請告訴我。
獲取方法:
HttpClient client = new HttpClient();
client.BaseAddress = new Uri("http://10.0.2.2:19367/api/");
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
HttpResponseMessage response = client.GetAsync("APICustAccount/getTestData").Result;
if (response.StatusCode == HttpStatusCode.OK)
{
var result = await response.Content.ReadAsStringAsync();
var todoItems = JsonConvert.DeserializeObject<List<CustomerApp>>(result);
}
我測試了上面的代碼,它對我有用。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.