Xamarin HttpClient.GetStringAsync在Xamarin.Droid上不起作用 [英] Xamarin HttpClient.GetStringAsync not working on Xamarin.Droid
问题描述
我真的是Xamarin开发的新手,我刚尝试使用Xamarin.Forms开发Android&具有共享代码的iOS App,我在这里要做的是使用Plugin.RestClient NuGet包从API获取数据.
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.
这是我的MainViewModel.
Here's my MainViewModel.
public MainViewModel(){
InitializeDataAsync();
}
public async Task InitializeDataAsync(){
var employeeServices = new EmployeeServices();
EmployeesList = await employeeServices.getEmployeesAsync();
}
这是我的服务
public async Task<List<Employee>> getEmployeesAsync(){
RestClient<Employee> restClient = new RestClient<Employee>();
var employeeList = await restClient.GetAsync("http://localhost:3000/employee");
return employeeList;
}
这是我的RestClient
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;
}
该代码在我的iPhone模拟器上正常工作.我可以在Debug中看到url和json响应.但是,在我的Android模拟器上,列表上没有任何内容.我什至找不到针对url和json响应的调试程序,我已经检查了我的应用程序输出和设备日志(我使用Xamarin Studio for Mac顺便说一句).
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).
我还已经在我的Android清单上添加了互联网许可. 我该怎么办?
I also already add internet permission on my Android Manifest. What should I do?
推荐答案
您只能在本地计算机上访问localhost
.您需要将localhost
更改为托管api的计算机的IP地址.
You can only access localhost
on your local machine. You need to change localhost
to the IP address of the machine on which your api is hosted.
因此
http://localhost:3000/employee
应该类似于
http://192.168.0.5:3000/employee
这篇关于Xamarin HttpClient.GetStringAsync在Xamarin.Droid上不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!