【问题标题】:Xamarin Forms and Asp.Net Web ApiXamarin 表单和 Asp.Net Web Api
【发布时间】:2018-11-08 18:10:22
【问题描述】:

我一直在关注关于将我的 xamarin 表单应用程序连接到 asp.net web api 的 youtube 教程。不幸的是,我的 Listview 没有被来自 api 的数据填充。

Xamarin 表单应用具有以下文件:

RestClient.cs

public class RestClient<T>    {
         private const string WebServiceUrl = "http://localhost:49864/api/Oppotunities/";

         public async Task<List<T>> GetAsync()
         {
             var httpClient = new HttpClient();

             var json = await httpClient.GetStringAsync(WebServiceUrl);

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

             return OppotunityList ;
         } }

MainViewModel.cs

 public MainViewModel()
           {
               InitializeDataAsync();

           }

           private async Task InitializeDataAsync()
           {
               var oppotunitiesServices = new OppotunitiesServices();
               OppotunitiesList = await oppotunitiesServices.GetOppotunitiesAsync();

           }

OppotunityServices.cs

public class OppotunitiesServices
    {
        public async Task<List<Oppotunity>> GetOppotunitiesAsync()
        {


            RestClient<Oppotunity> restClient = new RestClient<Oppotunity >();
            var oppotunitiesList =  await restClient.GetAsync();


            return oppotunitiesList;
        }

    }

【问题讨论】:

  • 您是否正在使用模拟器进行任何更改?如果是这样,您不应该使用 localhost 连接到您的环境
  • 是的。我正在使用模拟器。赦免。请问我可以用什么
  • 使用运行您的 Web 服务的机器的实际 IP 地址。像 http://192.168.0.101:49864/api/Oppotunities/

标签: c# asp.net xamarin.forms


【解决方案1】:

如果您从模拟器进行调试,则不应使用localhost 访问您的开发机器。您必须使用 IP 地址或正在运行的服务。

您可以直接从模拟器的浏览器测试 IP 地址,这样您就不会浪费时间启动/停止应用程序来调试...

希望对你有帮助

【讨论】:

  • 我仍然无法使用 IP 地址测试我的 Web API。当我运行当前托管在本地主机上的 Web API 服务时:
  • 我很难测试我的 IP 地址。以前我正在运行 web api(在本地运行)然后在我的浏览器上输入 localhost:49864/api/Oppotunities 我会的。现在我用 169.254.216.197/16 替换了我的本地主机,这是网络名称以太网下的 IPV4 地址,浏览器返回错误“无法访问站点”。我测试的方法不对吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-03-21
  • 2018-12-17
  • 1970-01-01
  • 2020-06-04
  • 1970-01-01
相关资源
最近更新 更多