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";
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()
{
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"