2017-04-07 31 views
1

我想通過Xamarin來調用Web API(這是我本地視覺工作室)形成 ,但它不是叫我的網頁API。我正在使用Visual Studio的Android模擬器。xamarin形式沒有要求使用Visual Studio的網頁API 2105

我使用的Visual Studio 2015年更新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(); 
    } 
+1

您的應用程序有互聯網的權限?一世。即'<使用許可權的android:name =「android.permission.INTERNET對」 />'在AndroidManifest.xml –

+0

你能達到從仿真瀏覽器的網址? –

+0

是的,我已經上網權限@tamas薩博 – satyender

回答

0

你好,請嘗試下面的代碼,讓我知道,如果你有任何查詢。

GET方法:

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); 
} 

我已經測試了上面的代碼,它的工作對我來說。

相關問題