How to send Lists with Retrofit 2.0 via POST

Asked

Viewed 575 times

1

I have a list of records and need to send to the server via POST, use Retrofit 2.0 to make the requests. Be able to make all requests, but I’m not able to send the list.

My interface:

private interface SalesService{

   @POST(API_BASE + "/registrar")
   Call<List<Registro>> sendRegisters(@Body List<Registro> list);

}

Method of Requesting Records:

public void senRegisters(List<Registro> registros) throws IOException {
    SalesService service = requestApi.create(SalesService.class);
    Call call = service.sendRegisters(registros);
    call.execute().body();
}

I am running the sendRegister() method in an Intentservice.

The server side was created using PHP Slim Framework. The method I received the request for is:

$app->group('/api/escolas', function() use ($app){
    //recebe registros das turma e salva no banco de dados
    $app->post('/registrar', function() use ($app){
        $request = $app->request;
        $registros = json_decode($request->getBody());
        foreach($registros as $reg){
            $class = $reg->turma;
            $count = $reg->qtdAlunos;
            $app->db->insertRegister($count, $class);
        }
        exit;
    });
});

Can someone help me with that?

1 answer

0


I solved the problem by changing the list of List<Registro> by an array Registro []

Browser other questions tagged

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