C# xamarin表单未使用visual studio 2015调用web api

C# xamarin表单未使用visual studio 2015调用web api,c#,xamarin,xamarin.android,xamarin.forms,C#,Xamarin,Xamarin.android,Xamarin.forms,我想通过Xamarin表单调用web api(在我本地的visual studio上) 但它并没有调用我的web api。我正在使用VisualStudioAndroid模拟器 我正在使用visual studio 2015 update 3,以下是我的代码 public class DataService { HttpClient client = new HttpClient(); string apiPaht = "http://10.0.2.2:19367/api/"

我想通过Xamarin表单调用web api(在我本地的visual studio上) 但它并没有调用我的web api。我正在使用VisualStudioAndroid模拟器

我正在使用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;
    }

}
公共类数据服务
{
HttpClient=新的HttpClient();
字符串APIPHT=”http://10.0.2.2:19367/api/";
//字符串APIPHT=”http://localhost:19367/api/";
公共异步任务GetTodoItemsAsync()
{
var response=await client.GetStringAsync(apipht+“APICustAccount/getTestData”);
var todoItems=JsonConvert.DeserializeObject(响应);
返回到doitems;
}
}
我的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();
    }
async void RefreshData()
{
List listCust=wait_dService.GetTodoItemsAsync();
todoList.ItemsSource=listCust;
字符串aa=“”;
}
受保护的无效btn_单击(对象发送方,事件参数e)
{ 
刷新数据();
}

您好,请尝试下面的代码,如果您有任何疑问,请告诉我

获取方法:

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);
}
HttpClient=newhttpclient();
client.BaseAddress=新Uri(“http://10.0.2.2:19367/api/");
client.DefaultRequestHeaders.Accept.Add(新的MediaTypeWithQualityHeaderValue(“应用程序/json”);
HttpResponseMessage response=client.GetAsync(“APICustAccount/getTestData”).Result;
if(response.StatusCode==HttpStatusCode.OK)
{
var result=await response.Content.ReadAsStringAsync();
var todoItems=JsonConvert.DeserializeObject(结果);
}

我已经测试了上面的代码,它对我有效。

您好,请尝试下面的代码,如果您有任何疑问,请告诉我

获取方法:

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);
}
HttpClient=newhttpclient();
client.BaseAddress=新Uri(“http://10.0.2.2:19367/api/");
client.DefaultRequestHeaders.Accept.Add(新的MediaTypeWithQualityHeaderValue(“应用程序/json”);
HttpResponseMessage response=client.GetAsync(“APICustAccount/getTestData”).Result;
if(response.StatusCode==HttpStatusCode.OK)
{
var result=await response.Content.ReadAsStringAsync();
var todoItems=JsonConvert.DeserializeObject(结果);
}

我已经测试了上述代码,它对我有效。

您的应用程序是否具有internet权限?我e<代码>在AndroidManifest.xml中,你能从emulator浏览器访问该URL吗?是的,我有internet权限@tamas Szabo,你能通过浏览器(从emulator或从你的电脑)访问API吗?我是移动开发新手,不知道如何使用emulator浏览器。请告诉我使用emulator Browser您的应用程序是否具有internet权限?我e<代码>在AndroidManifest.xml中,你能从emulator浏览器访问该URL吗?是的,我有internet权限@tamas Szabo,你能通过浏览器(从emulator或从你的电脑)访问API吗?我是移动开发新手,不知道如何使用emulator浏览器。请告诉我如何使用emulator浏览器