Consuming WEB API REST GET Xamarin Forms

2019-09-22 06:51发布

问题:

I have a WEB API hosted on a server, there I have a Products table with Name and Description.I already checked for the postman and this is ok, when I try to implement the method in xamarin by visual studio to bring a record by its name and display in a listview I can not. Could someone help me in the code

private async void GetProductByName(string Name)
    {
        using (var client = new HttpClient())
        {

            txtTest.Text = "http://www.ProdutosAPITest6.hostname.com/api/products";

            var URI = txtTest.Text + "/" + Name.ToString();

            var response = await client.GetAsync(URI);

            string products = await response.Content.ReadAsStringAsync();

            var product = JsonConvert.DeserializeObject<Produto>(products);

            listview.ItemsSource = products;

        }
    }



    <ListView x:Name="ProductsList" ItemSelected="listaProducts_ItemSelected" 
       BackgroundColor="Aqua" SeparatorColor="Blue">
            <ListView.ItemTemplate>
                <DataTemplate>
                    <ViewCell>

                        <StackLayout Padding="10,10" Orientation="Horizontal">
                            <Label Text="{Binding Id}" HorizontalOptions="StartAndExpand"/>
                            <Label Text="{Binding Name}" TextColor="Blue" HorizontalOptions="Center"/>
                            <Label Text="{Binding Description}" HorizontalOptions="End"/>
                        </StackLayout>
                    </ViewCell>
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>

回答1:

I made a little edit to your code.

public async void GetProductByName(string Name)
    {
                var handler = new NativeMessageHandler();
                var client = new HttpClient(handler);
                client.Timeout = TimeSpan.FromSeconds(120);

                txtTest.Text = "http://www.ProdutosAPITest6.hostname.com/api/products";
                var URI = txtTest.Text + "/" + Name.ToString();

                var requestMessage = new HttpRequestMessage
                {
                    Method = HttpMethod.Get,
                    RequestUri = new Uri(URI)
                };

                var response = await client.SendAsync(requestMessage);

                if (response.IsSuccessStatusCode == true)
                {
                    var products = await response.Content.ReadAsStringAsync();

                    var resultModel = JsonConvert.DeserializeObject<Produto>(products);
                    return resultModel;
                }
    }

public ListView myListView { get { return ProductsList; } }

protected override async void OnAppearing()
    {
                var name = //pass name;
                List<ProductModel> products = await GetProductByName(name);

                if (products.Count() > 0)
                { 
                     myListView.ItemsSource = products;
                }

           base.OnAppearing();
   }

I hope this helps