Click here to Skip to main content
15,888,026 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more: , +
Hi! I have a xamarin.forms application which I added a WebService class that would get the list of users in my database.For the API,I created a asp.net core web application where I connected my local sqlite database to.This is my WebService class:
public class WebService
   {
       public static string BaseAddress =
   Device.RuntimePlatform == Device.Android ? "https://10.0.2.2:5001" : "http://192.168.87.107:5000";//using my local IP address and check to see the platform the app is running on
       public static string UsersUrl = $"{BaseAddress}/api/Users/";



       public async Task<ObservableCollection<User>> GetUser()
       {
           var httpClient = new HttpClient();
           var json = await httpClient.GetStringAsync(UsersUrl);
           var users = JsonConvert.DeserializeObject<ObservableCollection<User>>(json);
           return users;
       }

   }


This is my UsersViewModel class:
public class UsersViewModel : BaseViewModel
  {
      private WebService service = new WebService();
      public ICommand FetchDataUser { get; set; }
      public UsersViewModel()
      {
        //  FetchDataUser = new Command(FetchData);
          userService = new UserRepository();
          usersList = new ObservableCollection<User>();
          GetUsers();

      }
      private ObservableCollection<User> usersList;
      public ObservableCollection<User> UserList
      {
          get
          {
              return usersList;
          }
          set
          {
              if (usersList != null)
                  usersList = value;
              OnPropertyChanged("UserList");
          }
      }
     private async Task GetUsers()
      {
         usersList = await service.GetUser();

      }
  }

When I run the debugger and I put the breakpoint,I can see only the url used by the application without any other records.In the .Net application,I can see the records in the database without any problem,both in Postman and in the browser so I have no idea why is this behavior. Can someone help me in this matter?Any help would be appreciated.

What I have tried:

I tried to iterate through the list of users and add the records to the collection:
private async Task GetUsers()
      {
         usersList = await service.GetUser();
          foreach(User r in usersList)
          {
              usersList.Add(r);
          }
      }


I have tried to change my local IP address with both "localhost" and "127.0.0.1" and to declare a separate url that would have the api of the user list:
public string url = "http://127.0.0.1:5000/api/Users"
Posted
Updated 19-May-20 6:40am

1 solution

I have managed to find the solution for it.I used a simple url declaration but instead of 127.0.0.1 i used the localhost for the Android emulator which is
10.0.2.2
and it looks like this:
public string url = "http://10.0.2.2:5000/api/Users"
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900