Error sending Arraylist via POST in Retrofit 2: java.lang.Illegalstateexception: Expected BEGIN_ARRAY but was STRING

Asked

Viewed 267 times

1

When I try to send an Arraylist via @POST to be saved to my webserver with Retrofit2, Arraylist is saved, but I am getting the following error:

java.lang.Illegalstateexception: Expected BEGIN_ARRAY but was STRING at line 1 column 1 path $

What can I do to fix this mistake? I think it has something to do with Gson, but I’m a beginner and I’m a little lost.

This is the Arraylist I’m trying to send:

ArrayList<ModelContato> listContatos = new ArrayList<>();

    ModelContato c = new ModelContato("5", "ATIVO", "TESTE", "12134567", "14646", "[email protected]", "Teste");
    listContatos.add(c);
    c = new ModelContato("6", "INATIVO", "TESTE2", "12123456", "14646", "[email protected]", "Teste2");
    listContatos.add(c);

And this is my Call that runs the upload:

Call<List<ModelContato>> callM = contatoInterface.createRContato(listContatos);
    callM.enqueue(new Callback<List<ModelContato>>() {
        @Override

    public void onResponse(Response<List<ModelContato>> response, Retrofit retrofit) {
            Log.i("TAG", "Salvo com sucesso");
        }

        @Override
        public void onFailure(Throwable t) {
            Log.i("TAG", "Erro ao salvar: " + t.getMessage());
        }
    });

This is my interface:

public interface ContatoInterface {

@GET("recebe")
Call<List<ModelContato>> getRContatos();

@POST("envia")
Call<List<ModelContato>> createRContato(@Body ArrayList<ModelContato> modelContato);}

1 answer

1

Modify the feedback you want to receive in your interface. We have:

@POST("envia")
Call<List<ModelContato>> createRContato(@Body ArrayList<ModelContato> modelContato);}

This means that after performing the post you expect to receive a List from the server. However, you are getting a string as it says the error you got.

So just check what you are sending back as a response when creating the user and modifying its interface, I think the following should solve:

 @POST("envia")
    Call<String> createRContato(@Body ArrayList<ModelContato> modelContato);}

Browser other questions tagged

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