Help to get response values on Android with Retrofit

-1

I'm trying to use Retrofit to consume a feature but at the time of using the values I'm doing something wrong because nothing appears. Excerpt in the search:

        final TextView codigoCliente = findViewById(R.id.codigo_cliente);
    final TextView nomeCliente = findViewById(R.id.nome_cliente);

    Call<Pedido> json = new RetrofitInicializador().getPedidoService().buscaPedido();
    json.enqueue(new Callback<Pedido>() {
        @Override
        public void onResponse(Call<Pedido> call, Response<Pedido> response) {
            Pedido pedidoResponse = response.body();
            codigoCliente.setText(pedidoResponse.getId_cliente());
            nomeCliente.setText(pedidoResponse.getCliente());
        }

        @Override
        public void onFailure(Call<Pedido> call, Throwable t) {
        }
    });

Order Class

package com.br.site.Separacao;

import java.io.Serializable;

class Pedido implements Serializable {
    private int id_cliente;
    private String cliente;
    private String data;
    private int pares;

    public int getId_cliente() {
        return id_cliente;
    }

    public void setId_cliente(int id_cliente) {
        this.id_cliente = id_cliente;
    }

    public String getCliente() {
        return cliente;
    }

    public void setCliente(String cliente) {
        this.cliente = cliente;
    }

    public String getData() {
        return data;
    }

    public void setData(String data) {
        this.data = data;
    }

    public int getPares() {
        return pares;
    }

    public void setPares(int pares) {
        this.pares = pares;
    }
}

Interface class:

public interface PedidoService {
    @GET("separacao/pedido")
    Call<Pedido> buscaPedido();
}

In the android log, the query was successful.

 D/OkHttp: <-- 200 OK http://192.168.0.134/site/api/separacao/pedido (234ms)
    Date: Fri, 21 Dec 2018 12:11:02 GMT
    Server: Apache/2.4.37 (Win32) OpenSSL/1.1.1 PHP/7.2.12
    X-Powered-By: PHP/7.2.12
D/OkHttp: Set-Cookie: PHPSESSID=vvfg2h4jv865tltfg3amv4abgh; expires=Tue, 20-Dec-2022 12:11:02 GMT; Max-Age=126144000; path=/
    Expires: Thu, 19 Nov 1981 08:52:00 GMT
    Cache-Control: no-store, no-cache, must-revalidate
    Pragma: no-cache
    Content-Length: 256
    Keep-Alive: timeout=5, max=100
    Connection: Keep-Alive
    Content-Type: application/json
D/OkHttp: [{"id_cliente":"31","0":"31","cliente":"Cliente teste","1":"Cliente teste","data_hora":"2018-12-17 15:06:12","2":"2018-12-17 15:06:12","pares":"1","3":"1","prioridade_separacao":"1","4":"1","separador_temp":"0","5":"0"}]
    <-- END HTTP (256-byte body)

But I can not insert the values in the textView. Does anyone know why?

    
asked by anonymous 21.12.2018 / 13:18

2 answers

0

As far as I can tell, its json is what is the problem for retrofit :

[ // <-- problema

    {
        "id_cliente":"31",
        "0":"31",
        "cliente":"Cliente teste",
        "1":"Cliente teste",
        "data_hora":"2018-12-17 15:06:12",
        "2":"2018-12-17 15:06:12",
        "pares":"1",
        "3":"1",
        "prioridade_separacao":"1",
        "4":"1",
        "separador_temp":"0",
        "5":"0"
    }

]  // <-- problema

When you have the object inside brackets, you have a list. An array.

Or you create a simple json , like this:

{
    "id_cliente":"31",
    "0":"31",
    "cliente":"Cliente teste",
    "1":"Cliente teste",
    "data_hora":"2018-12-17 15:06:12",
    "2":"2018-12-17 15:06:12",
    "pares":"1",
    "3":"1",
    "prioridade_separacao":"1",
    "4":"1",
    "separador_temp":"0",
    "5":"0"
}

Or you create a list as shown in the @LeonardoDias example

    
21.12.2018 / 13:46
1

Attempt to change all Order to List < Request >

Example:

Call<List<Pedido>> json = new RetrofitInicializador().getPedidoService().buscaPedido();
    json.enqueue(new Callback<List<Pedido>>() {
        @Override
        public void onResponse(Call<List<Pedido>> call, Response<List<Pedido>> response) {
            Pedido pedidoResponse = response.body();
            codigoCliente.setText(pedidoResponse.getId_cliente());
            nomeCliente.setText(pedidoResponse.getCliente());
        }

        @Override
        public void onFailure(Call<List<Pedido>> call, Throwable t) {
        }
    });
    
21.12.2018 / 13:23