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

1

When I try to send an ArrayList via @POST to be saved in my webserver with Retrofit2, the ArrayList is saved, however 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 resolve this error? I think it has something to do with Gson, but I'm a beginner and I'm kind of 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 makes sending:

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);}
    
asked by anonymous 29.12.2015 / 14:34

1 answer

1

Modify the return you want to receive on your interface. We have:

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

This means that after completing 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're sending back as a response when creating the user and modify their interface, I think the following should resolve:

 @POST("envia")
    Call<String> createRContato(@Body ArrayList<ModelContato> modelContato);}
    
22.03.2016 / 14:21