Problems making a call using Retrofit on Android

1

I am a student and I have difficulty understanding where I am going wrong in a job that the teacher has passed. Describing briefly, the teacher provided an API in GitHub, which after completing the installation we can add products, suppliers and customers (using the PostMan app). Follow the API link - link . It uses the link address. Anyway, the job is to make a CRUD using the VOLLEY library or the RETROFIT library at our choice. In Volley I could not do anything. In Retrofit I get access to the API find method, but I can not show the list in my Activity.

Product List class code

    public class ProdutosLista extends AppCompatActivity {

ProgressDialog dialog;

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


}

@Override
protected void onStart(){
    super.onStart();
    final ListView lista = (ListView)findViewById(R.id.lvProdutos);

    ApiInterface apiInterface = ApiInterface.retrofit.create(ApiInterface.class);
    /*
    ==============================================================================
    Charging... para evitar açoes indesejadas na aplicação
    ==============================================================================
     */

    dialog = new ProgressDialog(ProdutosLista.this);
    dialog.setMessage("Carregando...");
    dialog.setCancelable(false);
    dialog.show();

    final Call<List<Produto>> call = apiInterface.getProdutos();

    call.enqueue(new Callback<List<Produto>>() {

        @Override
        public void onResponse(Call<List<Produto>> call, Response<List<Produto>> response) {

            if (dialog.isShowing())
                dialog.dismiss();

            final List<Produto> listaProdutos = response.body();
            if (listaProdutos != null){
                ProdutosAdapter adapter = new ProdutosAdapter(getBaseContext(), listaProdutos);

                lista.setAdapter(adapter);


            }

        }

        @Override
        public void onFailure(Call<List<Produto>> call, Throwable t) {

            if (dialog.isShowing())
                dialog.dismiss();

            Toast.makeText(getBaseContext(), "Problema de acesso", Toast.LENGTH_LONG).show();

        }
    });

}

}

This is the Interface code:

    public interface ApiInterface {

@GET ("/products")
Call<List<Produto>>getProdutos();

public static final Retrofit retrofit = new Retrofit.Builder()
    .baseUrl("http://10.0.2.2:3033")
    .addConverterFactory(GsonConverterFactory.create())
    .build();

}

This is the Product template class

    public class Produto {

String code;
String name;
int price;
String description;
int inventoryActual;
String supplier;

public int getPrice() {
    return price;
}

public void setPrice(int price) {
    this.price = price;
}

public int getInventoryActual() {
    return inventoryActual;
}

public void setInventoryActual(int inventoryActual) {
    this.inventoryActual = inventoryActual;
}

public String getCode() {
    return code;
}

public void setCode(String code) {
    this.code = code;
}

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public String getDescription() {
    return description;
}

public void setDescription(String description) {
    this.description = description;
}

public String getSupplier() {
    return supplier;
}

public void setSupplier(String supplier) {
    this.supplier = supplier;
}

}

This is the Adapter class

    public class ProdutosAdapter extends ArrayAdapter<Produto>{

private final Context context;
private final List<Produto> elementos;

public ProdutosAdapter(Context context, List<Produto> elementos) {
    super(context, R.layout.linha, elementos);
    this.context = context;
    this.elementos = elementos;
}

    @Override
    public View getView(int position, View convertView, ViewGroup parent){

        LayoutInflater inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        View rowView = inflater.inflate(R.layout.linha, parent, false);

        TextView name = (TextView)rowView.findViewById(R.id.name);
        TextView code = (TextView)rowView.findViewById(R.id.code);
        TextView price = (TextView)rowView.findViewById(R.id.price);
        TextView description = (TextView)rowView.findViewById(R.id.description);
        TextView inventoryActual = (TextView)rowView.findViewById(R.id.inventoryActual);
        TextView supplier = (TextView)rowView.findViewById(R.id.supplier);

        /*
        ==================================================================================
        Set values
        ==================================================================================
         */

        name.setText(elementos.get(position).getName());
        code.setText(elementos.get(position).getCode());
        price.setText(Integer.toString(elementos.get(position).getPrice()));
        description.setText(elementos.get(position).getDescription());
        inventoryActual.setText(Integer.toString(elementos.get(position).getInventoryActual()));
        supplier.setText(elementos.get(position).getSupplier());

        return  rowView;

    }

}

And finally this is the XML of the custom list layout

    <?xml version="1.0" encoding="utf-8"?>

<LinearLayout
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:orientation="vertical">

    <TextView
        android:id="@+id/name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="top"
        android:paddingRight="16dp"
        android:textSize="16sp"
        android:textStyle="bold" />


    <TextView
        android:id="@+id/code"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:paddingRight="16dp" />

    <TextView
        android:id="@+id/description"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:maxLines="3"
        android:paddingRight="16dp"
        />
    <TextView
        android:id="@+id/supplier"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:maxLines="3"
        android:paddingRight="16dp"
        />
    <TextView
        android:id="@+id/inventoryActual"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:maxLines="3"
        android:paddingRight="16dp"
        />

</LinearLayout>

<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="35dp"
    android:orientation="horizontal">


    <TextView
        android:id="@+id/price"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="8dp"
        android:text="$" />

</LinearLayout>

Here'sJson:

{"total": 2,
"limit": 10,
"skip": 0,
"data": [
    {
        "code": "002",
        "name": "Rims",
        "price": 45,
        "description": "R15 Corolla",
        "inventoryActual": 12,
        "supplier": "NCdaojXnykwxBV2n",
        "removed": null,
        "onlineStatus": null,
        "notes": null,
        "categId": null,
        "_id": "3qsvzvtpDc2JmAEM"
    },
    {
        "code": "001",
        "name": "Pneu",
        "price": 30,
        "description": "R15 Corolla",
        "inventoryActual": 10,
        "supplier": "qvql3wCvEInl5RDZ",
        "removed": null,
        "onlineStatus": null,
        "notes": null,
        "categId": null,
        "_id": "D9ete272UCGgjKDQ"
    }
]

}

    
asked by anonymous 07.11.2017 / 07:48

1 answer

2

Gson is not able to convert the response to the reported type. According to your JSON, you should create a class that involves the list of products:

public class RespostaProduto {

  private int total;
  private int limit;
  private int skip;
  private List<Produto> data;

  // Getters e setters
}

And change your interface:

@GET ("/products")
Call<RespostaProduto>getProdutos();
    
07.11.2017 / 18:30