I'm really new in Xamarin development, and I just tried Xamarin.Forms to Develop Android & iOS App with shared Code, and what I'm trying to do here is to get data from API with Plugin.RestClient NuGet package.
Here's my MainViewModel.
public MainViewModel(){
InitializeDataAsync();
}
public async Task InitializeDataAsync(){
var employeeServices = new EmployeeServices();
EmployeesList = await employeeServices.getEmployeesAsync();
}
And here's my Services
public async Task<List<Employee>> getEmployeesAsync(){
RestClient<Employee> restClient = new RestClient<Employee>();
var employeeList = await restClient.GetAsync("http://localhost:3000/employee");
return employeeList;
}
And this is my RestClient
public async Task<List<T>> GetAsync(string WebServiceUrl){
var httpClient = new HttpClient();
var json = await httpClient.GetStringAsync(WebServiceUrl);
Debug.WriteLine(WebServiceUrl);
Debug.WriteLine(json);
var taskModels = JsonConvert.DeserializeObject<List<T>>(json);
return taskModels;
}
The code works fine on my iPhone Emulator. I can see my Debug for the url and json response. But on my Android Emulator there's nothing on the list. I can't even find my Debug for url and json response, I already checked my Application Output and my Device Log (I used Xamarin Studio for Mac btw).
I also already add internet permission on my Android Manifest. What should I do?
If you are getting a timed out error then it's probably because "localhost" to the Android OS means the phone itself, which is probably not where you're not running the web server. There are some special IP addresses you can use while testing with Android emulators to reach the host OS:
You could try changing your URL based on the type of emulator you are using. Alternatively, you can plugin your workstations IP address instead, but this is less desirable because it can change.
If this doesn't allow you to reach the server then you may consider looking at any firewalls to see if your traffic is being blocked.
You can only access
localhost
on your local machine. You need to changelocalhost
to the IP address of the machine on which your api is hosted.So the api endpoint for
should be something like