Retrofit response coming in as null

Asked

Viewed 121 times

2

I am trying to make a retrofit call from a list of objects (Registrations) and in Httploggininterceptor the answer is appearing normally. What might be going on?

My call:

CadastroApi cadastroApi = retrofit.create(CadastroApi.class);

            Call<List<Cadastro>> callCadastros =cadastroApi.getCadastro(token);
            callCadastros.enqueue(new Callback<List<Cadastro>>() {
                @Override
                public void onResponse(Response<List<Cadastro>> response, Retrofit retrofit) {
                    List<Cadastro> cadastroList = response.body();

                    if (cadastroList != null){

                        Log.i("ACTTOKEN","Resultado da resposta cadastro: "+cadastroList.get(1).getCpf_cnpj());

                        // Insere o cadastro no banco de dados
                        try {
                            DBHelper dbHelper = new DBHelper(TokenActivity.this,token);
                            SQLiteDatabase conn = dbHelper.getWritableDatabase();
                            RepositorioCadastro repositorioCadastro = new RepositorioCadastro(conn);

                            for (Cadastro cadastro : cadastroList){

                                repositorioCadastro.inserirCadastro(cadastro);


                            }
                        }catch (SQLiteException e){

                            Log.i("ACTTOKEN","Erro ao inserir cadastro: "+e);

                        }

                    }

                }

                @Override
                public void onFailure(Throwable t) {
                    // Em caso de erro
                    Log.i("ACTTOKEN", "Erro retrofit ao baixar Cadastros: " + t.getCause());

                }

                });

My API:

public interface CadastroApi {

    @POST("Cadastro_Down.php?token=")
    Call<List<Cadastro>> getCadastro(@Query("token") String token);

}

Answer from the Webservice

[{
    "id": "-1",
    "data": null,
    "nome": "CLIENTE VENDA R\u00c1PIDA",
    "cpf_cnpj": "1111111111",
    "email": "[email protected]",
    "fone": null,
    "celular": null,
    "fax": null,
    "status": "1",
    "obs": null,
    "municipal": null,
    "estadual": null,
    "rg": null,
    "foto": null,
    "sexo": null,
    "data_nas": null,
    "nome_fantasia": null,
    "forca_venda": null,
    "id_vendedor": "2",
    "limite": "0.00",
    "casado": "",
    "pai": "",
    "mae": "",
    "fiado": "0.00"
}, {
    "id": "91",
    "data": "2016-01-09",
    "nome": "SAMUEL RAUL PINTO",
    "cpf_cnpj": "65573998455",
    "email": "[email protected]",
    "fone": "(69) 2925-9737",
    "celular": "(69) 8508-5128",
    "fax": "(69) 2925-9737",
    "status": "1",
    "obs": "f7WDsqCrfc",
    "municipal": "",
    "estadual": "",
    "rg": "37.922.970-5",
    "foto": "",
    "sexo": "MASCULINO",
    "data_nas": "1994-06-10",
    "nome_fantasia": "",
    "forca_venda": null,
    "id_vendedor": "1",
    "limite": "10000.00",
    "casado": "N\u00c3O",
    "pai": "AGENOR",
    "mae": "RORAIMA",
    "fiado": "5000.00"
}]
  • It may be that it receives the answer but cannot transform the answer into its object, it may happen by incompatible types in JSON, for example, if status for a Boolean in your java class, will not accept "1" as value, the same if you try to assign a null to an integer for example. try to take that same JSON without any null and see if it gets normally.

No answers

Browser other questions tagged

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