Retrieve json object

3

I have the following object:

{
  codigo : "1",
  nome   : "Carlos"
}

Java class (POJO)

public class usuario{
   @SerializedName("codigo")
   private int codigo;

   @SerializedName("nome")
   private String nome;

   /* ... */
}

The call

Call<Usuario> getUsuario(@Query("codigo")

How do I call this object using retrofit?

I'm trying like this:

private void getUsuario ( Service service, int codigo ){
        Call<Usuario> userCall = service.getUsuario( codigo );
        userCall.enqueue(new Callback<Usuario>() {
            @Override
            public void onResponse(Call<Usuario> call, Response<Usuario> response) {
                Log.i("User body",response.toString());
                if( response.isSuccessful() ){

                   //Não sei o que fazer 

                }

            }

            @Override
            public void onFailure(Call<Usuario> call, Throwable t) {
                Log.i("onFailure Usuario", t.getMessage());
            }
        });
    }
    
asked by anonymous 09.08.2017 / 16:20

2 answers

1

To get the result is response.body does this: User replieservice = response.body gives your instance the user class (the POJO you created) and saves it like this: user.setname = replieservice.getname, if it is from a list is a little different, all this within the response

    
11.08.2017 / 02:40
1

Body of your response is already your object!

Then try the following:

private void getUsuario ( Service service, int codigo ){
        Call<Usuario> userCall = service.getUsuario( codigo );
        userCall.enqueue(new Callback<Usuario>() {
            @Override
            public void onResponse(Call<Usuario> call, Response<Usuario> response) {
                Log.i("User body",response.toString());
                if( response.isSuccessful() ){

                   //VAMOS PEGAR O USUARIO
                   Usuario usuario = response.body();

                }

            }

            @Override
            public void onFailure(Call<Usuario> call, Throwable t) {
                Log.i("onFailure Usuario", t.getMessage());
            }
        });
    }
    
09.08.2017 / 23:35