Retrofit = manipulating the response

Well, I'm asking more that help from friends poi really, I'm not getting understand the dynamics.

Following tutorials and explanations here from the forum I'm trying to use Retrofit but it's not working.

I have the following JSON below,

{
  "clientes":
     [
        {"idClientesT":"1","tipo":"s","nome":"Carlos"},
        {"idClientesT":"2","tipo":"s","nome":"Rogério"}
     ]
} 

I get when I access URL below,

Http://hotplateprensas.com.br/ws/clientest.php

I'm using like this:

Interface:

package carcleo.com.radiosingular.classes;

import java.util.List;

import retrofit2.Call;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;
import retrofit2.http.GET;
import retrofit2.http.Path;

public interface ClientesI {
    @GET("clientest.php")
    Call<List<Clientes>> listClientes();
}

Activity:

package carcleo.com.radiosingular;

import android.os.Build;
import android.support.annotation.RequiresApi;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import java.util.List;

import carcleo.com.radiosingular.classes.Clientes;
import carcleo.com.radiosingular.classes.ClientesI;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class retrofit extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.retrofit);

        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl("http://www.hotplateprensas.com.br/ws/")
                .addConverterFactory(GsonConverterFactory.create())
                .build();

        ClientesI service = retrofit.create(ClientesI.class);


        service.listClientes().enqueue(new Callback<List<Clientes>>() {
            @Override public void onResponse(Call<List<Clientes>> call, Response<List<Clientes>> response) {

                int code = response.code();
                if (code == 200) {
                    for (Clientes cli : response.body()) {
                        Toast.makeText(getBaseContext(), "Id do cliente: " + cli.getIdClientesT(), Toast.LENGTH_LONG).show();
                        Toast.makeText(getBaseContext(), "Tipo do cliente: " + cli.getTipo(), Toast.LENGTH_LONG).show();
                        Toast.makeText(getBaseContext(), "Nome do cliente: " + cli.getNome(), Toast.LENGTH_LONG).show();
                    }
                } else {
                    Toast.makeText(getBaseContext(), "Falha: " + String.valueOf(code), Toast.LENGTH_LONG).show();
                }

            }

            @Override public void onFailure(Call<List<Clientes>> call, Throwable t) {
                Log.v("Errado: ", t.toString());
                Toast.makeText(getBaseContext(), t.getMessage(), Toast.LENGTH_LONG).show();
            }
        });

   }

}

A Class of clients:

package carcleo.com.radiosingular.classes;

public class Clientes {
    private int idClientesT;
    private String tipo;
    private String nome;

    public Clientes(int idClientesT, String tipo, String nome) {
        this.idClientesT = idClientesT;
        this.tipo = tipo;
        this.nome = nome;
    }

    public int getIdClientesT() {
        return idClientesT;
    }

    public String getTipo() {
        return tipo;
    }

    public String getNome() {
        return nome;
    }
}

The goal is to traverse the array JSON obtained by accessing the webservice and returning a list of objects of the Class customers .

Error only occurs at runtime.

But for me the error is coding same. Logic.

Obs.: I not with me receive

code == 200

Is going straight into this block:

@Override public void onFailure(Call<List<Clientes>> call, Throwable t) {
   Log.v("Errado: ", t.toString());
   Toast.makeText(getBaseContext(), t.getMessage(), Toast.LENGTH_LONG).show();
}

But I can't handle the return (response)

Console error RUN :

W/art: Before Android 4.1, method int android.support.v7.widget.DropDownListView.lookForSelectablePosition(int, boolean) would have incorrectly overridden the package-private method in android.widget.ListView
D/OpenGLRenderer: endAllActiveAnimators on 0x9a465680 (RippleDrawable) with handle 0x9a5fd370
V/Errado:: java.lang.IllegalStateException: Expected BEGIN_ARRAY but was BEGIN_OBJECT at line 1 column 2 path $

Can anyone help me?

Author: Carlos Rocha, 2018-12-21

1 answers

The Class returned in your service's method should hit with the JSON representation that the API returns.

Create a class that represents JSON:

import java.util.List;

class ClientesResponse {

  private List<Clientes> clientes;

  public ClientesResponse(List<Clientes> clientes) {
    this.clientes = clientes;
  }

  public List<Clientes> getClientes() {
    return clientes;
  }

  public void setClientes(List<Clientes> clientes) {
    this.clientes = clientes;
  }
}

And use it in the Retrofit return:

Call<ClientesResponse> listClientes();
 1
Author: Leonardo Lima, 2018-12-21 12:13:46