Help to pick up response values on Android with Retrofit

Asked

Viewed 186 times

-1

I am trying to use Retrofit to consume a resource but when using the values I am doing something wrong because nothing appears. Section in I make 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) {
        }
    });

Request 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 appears that 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 cannot insert the values into the textView. Does anyone know why?

2 answers

1


From what I understand, yours json is the problem for the 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 square brackets, you have a list. An array.

Or you create a json simple, 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

  • Really. That was a problem. But after I fixed the json I now fell into the same error when it was a list. The error falls on the line " codeClient.setText(requestResponse.getId_client());"

  • @Bruno but what is written in the error?

  • 1

    I just solved it. codigoCliente.setText(String.valueOf(requestResponse.getId_client())); Anyway, this json tuning infection was of great help. Thank you.

  • @Bruno quiet. =)

1

Try to change all the Request for 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) {
        }
    });
  • But there’s a reason?

  • @Bruno the reason is that your json is a list. But you need to implement for inside onresponse

  • But in my interface class I had done " @GET("separation/request") Call<Requested> searchPedido();" For me it had become an object. So Retrofit always returns a list?

  • It got worse. Now the error app opens the screen. In fact my feature will always be a single object. I saw no point in using a List

Browser other questions tagged

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