Xamarin PCL项目中的HttpClient访问失败

时间:2016-12-30 15:56:33

标签: c# xamarin visual-studio-2015 xamarin.android xamarin.forms

我的Xamarin PCL项目中有这段代码。但是,在第4行,它意味着调用GetStringAsync方法,代码在该点退出该方法并且不返回任何响应。我无法从Web服务获取json数据,并尝试了几种解决方法但没有成功。我正在使用Visual Studio 2015。

  //this calls the webservice
  public class RestClient<T>
   {
    private const string WebServiceUrl = "http://localhost:14241/api/Employees/";

    public async Task<List<T>> GetAsync()
    {
        var httpClient = new HttpClient();

        var json = await httpClient.GetStringAsync(WebServiceUrl);

        var taskModels = JsonConvert.DeserializeObject<List<T>>(json);

        return taskModels;
    }

    public async Task<bool> PostAsync(T t)
    {
        var httpClient = new HttpClient();

        var json = JsonConvert.SerializeObject(t);

        HttpContent httpContent = new StringContent(json);

        httpContent.Headers.ContentType = new MediaTypeHeaderValue("application/json");

        var result = await httpClient.PostAsync(WebServiceUrl, httpContent);

        return result.IsSuccessStatusCode;
    }

    public async Task<bool> PutAsync(int id, T t)
    {
        var httpClient = new HttpClient();

        var json = JsonConvert.SerializeObject(t);

        HttpContent httpContent = new StringContent(json);

        httpContent.Headers.ContentType = new MediaTypeHeaderValue("application/json");

        var result = await httpClient.PutAsync(WebServiceUrl + id, httpContent);

        return result.IsSuccessStatusCode;
    }

    public async Task<bool> DeleteAsync(int id, T t)
    {
        var httpClient = new HttpClient();

        var response = await httpClient.DeleteAsync(WebServiceUrl + id);

        return response.IsSuccessStatusCode;
    }
}


//this is the main view model that binds to the XAML page
public class MainViewModel : INotifyPropertyChanged
{
    private List<Employee> _employeeList;
    public List<Employee> EmployeesList
    {
        get { return _employeeList; }
        set
        {
            _employeeList = value;
            OnPropertyChanged();
        }
    }

    public MainViewModel()
    {
        InitializeDataAsync();
    }

    private async Task InitializeDataAsync()
    {
        var employeeServices = new EmployeesServices();

        EmployeesList = await employeeServices.GetEmployeesAsync();
    }

    public event PropertyChangedEventHandler PropertyChanged;

    protected virtual void OnPropertyChanged([CallerMemberName] string propertyName=null)
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }

}



    //method that calls the GetAsync() method to retrieve the employee list       
    //from the web service
    public class EmployeesServices
    {
    public async Task<List<Employee>> GetEmployeesAsync()
    {
        RestClient<Employee> restClient = new RestClient<Employee>();

        var employeesList = await restClient.GetAsync();

        return employeesList;
    }
}

1 个答案:

答案 0 :(得分:-1)

确保您在整个调用链中使用await一直到GetAsync()