Return Vector within JSON Retrofit Array

Asked

Viewed 216 times

0

How do I bring information from one vector inside another json-format vector to retrofit:

I have my return:

[
  [
    {"prato":"BROCOLIS C\/ SALSINHA"},
    {"prato":"ACELGA"},
    {"prato":"ALFACE + JERIMUM COZIDO"}
  ],
  [
    {"prato":"QUICHE DE ALHO PORO"},
    {"prato":"ENSOPADO DE FEIJ?O BRANCO"}
  ],
  [
    {"prato":"ARROZ BRANCO"},
    {"prato":"FEIJ?O CARIOCA"},
    {"prato":"ARROZ INTEGRAL"}
  ],
  [
   {"prato":"CALDO VERDE"}
  ]
]

My interface

@GET("nutricao/")    
Call<Pratos> getListPratos(
      @Query("acao") String acao,
      @Query("tipo_refeicao") int tipo,
      @Query("data") String data);

My Model

public class Pratos {

    @SerializedName("pratos")
    private ArrayList<Prato> pratos;

    public ArrayList<Prato> getPratos() {
        return pratos;
    }

    public void setPratos(ArrayList<Prato> pratos) {
        this.pratos = pratos;
    }
}

Continuation:

public class Prato {

    @SerializedName("prato")

    private String prato;

    public String getPrato() {
        return prato;
    }

    public void setPrato(String prato) {
        this.prato = prato;
    }

    public String toString(){
        return prato;
    }
}

My attempt to show:

Call<Pratos> listPratos = service.getListPratos("P", 5, "01/08/2017");
        listPratos.enqueue(new Callback<Pratos>() {
            @Override
            public void onResponse(Call<Pratos> call, Response<Pratos> response) {
                Log.i("onResponse Pratos",response.toString());
                 Pratos pratosResponse = response.body();
                if( pratosResponse  != null ){
                    ArrayList<Prato> listaPratos = pratosResponse.getPratos();
                    for(int i = 0; i < listaPratos.size(); i++ ){
                        Log.i("Prato", listaPratos.get( i ).toString());
                    }
                }
            }

            @Override
            public void onFailure(Call<Pratos> call, Throwable t) {
                Log.i("onFailure Prato", t.getMessage());
            }
        });
}

Only you’re making that mistake:

java.lang.Nullpointerexception: Attempt to invoke virtual method 'int java.util.Arraylist.size()' on a null Object Reference

1 answer

1


The way your JSON is, your interface should be:

@GET("nutricao/")    
Call<ArrayList<ArrayList<Pratos>>> getListPratos(
      @Query("acao") String acao,
      @Query("tipo_refeicao") int tipo,
      @Query("data") String data
);

Browser other questions tagged

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