1

Hi guys i got a problem with Expected a string but was BEGIN_OBJECT i i cant find a solution been browsing for 2 days now , need help to fix this thanks

im storing the json content on a DB but on my json i got a nested array ,

this is my 1 item on my json file

[
    {
    "id": "355",
    "indicaciones": "text",
    "efectos_secundarios": "text",
    "contraindicaciones": "",
    "precauciones_advertencias": "text",
    "interacciones": "",
    "nombre_principio_activo": "Decoquinato",
    "especies": [
                {
                 "especies_id": "1"
                },
                {
                 "especies_id": "2"
                },
                {
                 "especies_id": "3"
                },
                {
                 "especies_id": "4"
                },
                {
                 "especies_id": "9"
                }
               ]
    }
]

this is my model with contructor and getters and seters

    private int id;
    private String indicaciones;
    private String dosis_via_administracion;
    private String efectos_secundarios;
    private String contraindicaciones;
    private String precauciones_advertencias;
    private String nombre_principio_activo;
    private String especies;

my api

   @GET("formulario.txt")
    Call<List<FormularioModel>> getFormularios();

this is my retrofit, where im lossing my mind

    //store formilarion on database
    public void storeFormulariosToDb(){
        Retrofit retrofitCat = new Retrofit.Builder()
                .baseUrl(ApiForm.BASE_URL)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        ApiForm apicat = retrofitCat.create(ApiForm.class);
        Call<List<FormularioModel>> callCat = apicat.getFormularios();
        callCat.enqueue(new Callback<List<FormularioModel>>() {
            @Override
            public void onResponse(Call<List<FormularioModel>> callCat, Response<List<FormularioModel>> response) {
                List<FormularioModel> obtenercate =db.getALLFormulario();
                List<FormularioModel> cat = response.body();
                if(obtenercate.size() < cat.size()){
                    for(int i = 0; i < cat.size(); i++){
                        FormularioModel cat1 = new FormularioModel(
                                cat.get(i).getId(),
                                cat.get(i).getIndicaciones(),
                                cat.get(i).getDosis_via_administracion(),
                                cat.get(i).getEfectos_secundarios(),
                                cat.get(i).getContraindicaciones(),
                                cat.get(i).getPrecauciones_advertencias(),
                                cat.get(i).getNombre_principio_activo(),
                                cat.get(i).getEspecies());  // Here is my problem


                        db.createFormularios(cat1);
                    }

                }

            }

            @Override
            public void onFailure(Call<List<FormularioModel>> callCat, Throwable t) {
                Log.e("obtener formulario", t.getMessage());            }
        });
    }

and this one is my DBHelper to store to db

 public long createFormularios(FormularioModel itemFORM){
        SQLiteDatabase db = this.getWritableDatabase();

        ContentValues valores = new ContentValues();
        valores.put(FORMULARIO_ID, itemFORM.getId());
        valores.put(FORMULARIO_INDICA, itemFORM.getIndicaciones());
        valores.put(FORMULARIO_VIA, itemFORM.getDosis_via_administracion());
        valores.put(FORMULARIO_EFECT, itemFORM.getEfectos_secundarios());
        valores.put(FORMULARIO_CONTRA, itemFORM.getContraindicaciones());
        valores.put(FORMULARIO_PRECA, itemFORM.getPrecauciones_advertencias());
        valores.put(FORMULARIO_NOMBRE, itemFORM.getNombre_principio_activo());
        valores.put(FORMULARIO_ESPECIE, itemFORM.getEspecies());


        long formulario_id = db.insert(TABLE_FORMULARIOS, null, valores);

        return formulario_id;

    }

and for read my db

 public List<FormularioModel> getALLFormulario(){
        List<FormularioModel> forms = new ArrayList<>();
        String selectQuery = " SELECT * FROM "
                + TABLE_FORMULARIOS
                + " ORDER BY "
                + FORMULARIO_NOMBRE + " ASC";

        SQLiteDatabase db = this.getReadableDatabase();
        Cursor c = db.rawQuery(selectQuery,null);

        if(c.moveToFirst()){
            do{
                FormularioModel t = new FormularioModel();
                t.setId(c.getInt(c.getColumnIndex(FORMULARIO_ID)));
                t.setIndicaciones(c.getString(c.getColumnIndex(FORMULARIO_INDICA)));
                t.setDosis_via_administracion(c.getString(c.getColumnIndex(FORMULARIO_VIA)));
                t.setEfectos_secundarios(c.getString(c.getColumnIndex(FORMULARIO_EFECT)));
                t.setContraindicaciones(c.getString(c.getColumnIndex(FORMULARIO_CONTRA)));
                t.setPrecauciones_advertencias(c.getString(c.getColumnIndex(FORMULARIO_PRECA)));
                t.setNombre_principio_activo(c.getString(c.getColumnIndex(FORMULARIO_NOMBRE)));
                t.setEspecies(c.getString(c.getColumnIndex(FORMULARIO_ESPECIE)));
                forms.add(t);
            } while (c.moveToNext());
        }
        return forms;
    }

i wants to save the especies array as String to be able to display it on my activity , thanks for the help

WarLion
  • 123
  • 1
  • 8
  • Possible duplicate of [Retrofit Expected BEGIN\_OBJECT but was BEGIN\_ARRAY](https://stackoverflow.com/questions/24154917/retrofit-expected-begin-object-but-was-begin-array) – sourav.bh Mar 23 '19 at 16:27

1 Answers1

2

problem is with your model class especies is not string its list of objects

in your model change private String especies; to private List<Especies> especies; and create a class named Especies like below

public class Especies {
    private String especies_id;
    ...
}