如何将数据从REST WebService发送到Xamarin App?

时间:2019-03-15 10:28:13

标签: c# api web-services xamarin webservice-client

我的程序需要帮助。我声明我仍然是初学者,如果问题很简单,请对不起。

问题:我已经使用SqlConnection开发了我的REST Web服务。我现在的问题是使我在xamarin表单应用程序(当前为静态)上拥有的数据动态化。

下面是我静态声明数据的代码的一部分:

public class MockDataStore: IDataStore <Item>
    {
        List <Item> items;

        // Allow me to assign values ​​to the declared variables

        public MockDataStore ()
        {
            items = new List <Item> ();
            var mockItems = new List <Item>
            {
                
                new Item {Id = Guid.NewGuid (). ToString (), Nome_cognome = "1xxx1", Email = "xxx", Phone = 3333333333, Role = "Programmer", ActivityMattina = "Is updating the databases", ActivitiesAfternoon = "Presentation ", Number_Inside = 123},
                               new Item {Id = Guid.NewGuid (). ToString (), Nome_cognome = "2xx2", Email = "xxx", Phone = 3333333333, Role = "Programmer", ActivityMattina = "Is updating the databases", ActivitiesAfternoon = "Presentation ", Number_Inside = 123},

                new Item {Id = Guid.NewGuid (). ToString (), Nome_cognome = "3xxx3", Email = "3xxx3", Phone = 3333333333, Role = "Programmer", ActivityMattina = "Is updating the databases", ActivitiesAfternoon = "Presentation ", Number_Inside = 123},
           };
            foreach (var item in mockItems)
            {
                Items.Add (item);
            }
        }

相反,这里是我在Web服务上运行的查询:

public List <Agenda> Rooms ()
        {
            List <Ansagenda> list = new List <Ansagenda> ();

            string queryString = "SELECT CDSISM, DESISM, TYPE, GSM, EMAIL, IP FROM [dbo]. [ANSIS00F] WHERE CDSISM = '44 'AND ANSIS00F.TIPO =' X ';";
            // string queryString = "SELECT CDSISM, DESISM, TYPE, GSM, EMAIL, IP, AGSIS, AGMAT, AGPOM, AGDAT, ORDER FROM [dbo]. [ANSIS00F] INNER JOIN [dbo]. [AGENDA] ON [CDSISM] = [AGSIS] WHERE ANSIS00F.TIPO = 'X' ORDER BY DESISM ASC, AGDAT ASC; ";

            SqlCommand command = new SqlCommand (queryString, connection);
            SqlDataAdapter adapter = new SqlDataAdapter (command);
            DataTable table = new DataTable ();

            connection.Open ();
            adapter.Fill (table);
            connection.Close ();

            foreach (DataRow dr in table.Rows)
            {
                list.Add (new Ansagenda
                {
                    CDSISM = Convert.ToString (dr ["CDSISM"]),
                    DESISM = Convert.ToString (dr ["DESISM"]),
                    TYPE = Convert.ToString (dr ["TYPE"]),
                    GSM = Convert.ToString (dr ["GSM"]),
                    EMAIL = Convert.ToString (dr ["EMAIL"]),
                    IP = Convert.ToString (dr ["IP"]),
                });
            }
            return list;
        }

1 个答案:

答案 0 :(得分:0)

如果要在应用程序中显示服务器中的数据,则需要以某种方式获取它们。您可以通过向服务器发出HTTP请求来实现。让我们从一个问题开始:您可以向您的服务器索取Agendas列表吗?您可以从浏览器/ POSTMAN中做到这一点吗?

我强烈建议您阅读this article,并尝试在您的应用中执行类似的操作。做到这一点并不难,并且使用Refit有很多优点。

当然,您可以自行发出请求,但是我不确定在有很多库可以这样做的情况下是否有意义。