How to connect Xamarin.Forms to Web Services?

Good Day everyone. I'm having trouble on how am I going to show all the records I have created in my ASP.NET WEB API to Xamarin.Forms Application. I tried creating pre-defined list of Employee's Name and Department and it worked. But what I want to do is Create a Record in ASP.NET Web Application and make it appear to my mobile application. Any help will be highly appreciated. Here's my code. I'm just a newbie so please forgive me if my question isn't seem to be informative. Thanks in advance. I watch a video tutorial regarding this matter. Refer to this link if needed. ()

MainViemMain.xaml
`

         <?xml version="1.0" encoding="utf-8" ?>
                     <ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
                                  xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
                                  x:Class="XamarinFormsDemo.MainPageMain"
                                  xmlns:ViewModels="clr-namespace:XamarinFormsDemo.ViewModels;assembly=XamarinFormsDemo">


                       <ContentPage.BindingContext>
                         <ViewModels:MainViewModel/>
                       </ContentPage.BindingContext>

          <StackLayout>
          <ListView ItemsSource="{Binding EmployeesList}"
                    HasUnevenRows="True">
            <ListView.ItemTemplate>
              <DataTemplate>
                <ViewCell>
                  <StackLayout Orientation="Vertical"
                               Padding="12,6">
                    <Label Text="{Binding Name}"
                           FontSize="24"/>

                    <Label Text="{Binding Department}"
                           FontSize="18"
                           Opacity="0.6"/>
                  </StackLayout>
                </ViewCell>
              </DataTemplate>
            </ListView.ItemTemplate>

          </ListView>

          <Label Text="Read This!"/>

          </StackLayout>

        </ContentPage>`

MainViewModel.cs

        `using System;
        using System.Collections.Generic;
        using System.ComponentModel;
        using System.Linq;
        using System.Runtime.CompilerServices;
        using System.Text;
        using System.Threading.Tasks;
        using XamarinFormsDemo.Models;
        using XamarinFormsDemo.Services;

        namespace XamarinFormsDemo.ViewModels
        {
            public class MainViewModel : INotifyPropertyChanged
            {

                private List<Employee> _employeesList;

                public List<Employee> EmployeesList
                {
                    get { return _employeesList; }
                    set
                    {
                        _employeesList = value;
                        OnPropertyChanged();
                    }
                }

                public MainViewModel()
                {
                    InitializeDataAsync();
                }

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

                    EmployeesList = await employeesServices.GetEmployeesAsync();
                }



                public event PropertyChangedEventHandler PropertyChanged;

                protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
                {
                    var handler = PropertyChanged;
                    if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
                }


            }
        }`

EmployeesServices.cs

                `
                using Plugin.RestClient;
                using System;
                using System.Collections.Generic;
                using System.Linq;
                using System.Text;
                using System.Threading.Tasks;
                using XamarinFormsDemo.Models;

                namespace XamarinFormsDemo.Services
                {
                    public class EmployeesServices
                    {


                        public async Task<List<Employee>> GetEmployeesAsync()
                        {
                            RestClient<Employee> restClient = new RestClient<Employee>();

                            var employeesList = await restClient.GetAsync();

                            return employeesList;

                        }


                    }
                }
                `

Answers

  • menontheChessboardmenontheChessboard TRMember ✭✭

    The request to get the employees does not work.

    One thing I noticed the second time trying the tutorial is when I add Plugin.RestClient, the System.Net.Http reference is not added.
    The other references are added. Could this be the problem. I tried to manually add the reference but could not.

    The following is what I have tried to figure out the problem.

    var employeesList = await restClient.GetAsync();

    in EmployeesServices class returns an empty/null employeeList.

    In the RestClient class.
    When I run the code and set a breakpoint in the GetAsync() method,

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

    is never reached.
    Do you have any ideas why this may be happening. Is it a async/await problem?
    Everything up to about 5 minutes left on the tutorial worked perfectly.

    IIS Express is running.
    For some reason, a connection with the server could not be established.

Sign In or Register to comment.