See External Restapi in Xamarinforms

Asked

Viewed 73 times

0

I am having difficulty to perform a query in an external Tinyerp Restapi to consult and include requests via an app created in my company, some difficulty is to use json because I am new to xamrin and an Asp.net framework application is working properly. Follow the json2csharp created model:

 public class Pedido2
    {
        public string id { get; set; }
        public string numero { get; set; }
        public object numero_ecommerce { get; set; }
        public string data_pedido { get; set; }
        public string data_prevista { get; set; }
        public string nome { get; set; }
        public double valor { get; set; }
        public string id_vendedor { get; set; }
        public string nome_vendedor { get; set; }
        public string situacao { get; set; }
        public string codigo_rastreamento { get; set; }
        public object url_rastreamento { get; set; }
    }

    public class Pedido1
    {
        public Pedido2 pedido { get; set; }
    }

    public class Retorno
    {
        public string status_processamento { get; set; }
        public string status { get; set; }
        public string pagina { get; set; }
        public string numero_paginas { get; set; }
        public List<Pedido1> pedidos { get; set; }
    }

    public class RootObject
    {
        public Retorno retorno { get; set; }
    }

and follow page.Cs:

    using MacVendas.Models.API;

using Newtonsoft.Json;
using Xamarin.Forms;
using Xamarin.Forms.Xaml;
using static MacVendas.Models.API.Pedido;

namespace MacVendas.Pages
{
    [XamlCompilation(XamlCompilationOptions.Compile)]
    public partial class ProdutoPage : ContentPage
    {

        public ProdutoPage()
        {
            InitializeComponent();
            //Iniciar();
            this.BindingContext = new Pedido();
        }
        private void Iniciar()
        {

            var client = new HttpClient();
            var resp = client.GetAsync("https://api.tiny.com.br/api2/pedidos.pesquisa.php?token=""&formato=json").Result;
            string respStr = resp.Content.ReadAsStringAsync().Result;

            RootObject ObjContactList = new RootObject();
            if (respStr != "")
            {
                //Converting JSON Array Objects into generic list  
                ObjContactList = JsonConvert.DeserializeObject<RootObject>(respStr);
            }
            //Binding listview with server response    
            listviewConacts.ItemsSource = ObjContactList.retorno.pedidos;
        }

    }
}

and the page.xmal:

 <Grid>
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>
        <Label Grid.Row="0" Margin="10" Text="JSON Parsing" FontSize="25" />
        <ListView x:Name="listviewConacts" Grid.Row="1" HorizontalOptions="FillAndExpand" HasUnevenRows="True" ItemSelected="listviewContacts_ItemSelected">
            <ListView.ItemTemplate>
                <DataTemplate>
                    <ViewCell>
                        <Grid HorizontalOptions="FillAndExpand" Padding="10">
                            <Grid.RowDefinitions>
                                <RowDefinition Height="Auto"/>
                                <RowDefinition Height="Auto"/>
                                <RowDefinition Height="Auto"/>
                                <RowDefinition Height="Auto"/>
                            </Grid.RowDefinitions>
                            <Label Text="{Binding nome}" HorizontalOptions="StartAndExpand" Grid.Row="0" TextColor="Blue"  FontAttributes="Bold"/>
                            <Label Text="{Binding numero}" HorizontalOptions="StartAndExpand" Grid.Row="1" TextColor="Orange"  FontAttributes="Bold"/>
                            <Label Text="{Binding }" HorizontalOptions="StartAndExpand" Grid.Row="2" TextColor="Gray"  FontAttributes="Bold"/>

                            <BoxView HeightRequest="2" Margin="0,10,10,0" BackgroundColor="Gray" Grid.Row="3" HorizontalOptions="FillAndExpand" />
                        </Grid>
                    </ViewCell>

                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>
    </Grid>
    <ActivityIndicator x:Name="ProgressLoader" IsRunning="True"/>
</Grid>

thanks in advance for the help

  • What is the difficulty you are having? I copied your code here and the object ObjContactList is completed correctly in the Json conversion.

  • So when I try to visualize the list nothing appears on my page xaml, it goes blank

  • The problem is in binding of ListView that is not taking the property pedido.nome and so on. If you can, please edit the title of your question to help other people search.

1 answer

1


Data is not being displayed on ListView why you missed placing the hierarchy of the object you want to display, for example: objeto1.objeto2.propriedade.

In this case the class Retorno has a list List<Pedido1> pedidos and each item Pedido1 has an object of the type Pedido2, and in turn the object Pedido2 has all the data from Pedido as nome and numero.

The ListView is receiving a list of Pedido1 class Retorno

listviewConacts.ItemsSource = ObjContactList.retorno.pedidos;

only that in the view the binding is as {Binding nome} and in class Pedido1 there is no property nome:

<Label Text="{Binding nome}" HorizontalOptions="StartAndExpand" Grid.Row="0" TextColor="Blue"  FontAttributes="Bold"/>

The attributes of the request (name, number, etc.) are all in the class Pedido2 which in turn is a class property Pedido1.

Correct: put on the binding of ListView the name of the class property Pedido1 (property pedido) who’s kind Pedido2.

<Label Text="{Binding pedido.nome}" HorizontalOptions="StartAndExpand" Grid.Row="0" TextColor="Blue"  FontAttributes="Bold"/>
<Label Text="{Binding pedido.numero}" HorizontalOptions="StartAndExpand" Grid.Row="1" TextColor="Orange"  FontAttributes="Bold"/>
  • It worked, thank you very much

  • Now the problem is that the listview ends before seeing all the items

Browser other questions tagged

You are not signed in. Login or sign up in order to post.