Retrieve json object

Asked

Viewed 260 times

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")

What 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());
            }
        });
    }
  • To get the result is Answer.body does like this: User replies service = Answer.body dai tu instance the user class (the POJO you created ) and saves like this: user.setname = answerservice.getname, if it is from a list it is a little different, this is all inside the Answer

  • Actually, my lists worked fine

  • Your way it worked!

  • 1

    Can you add as an answer, please

2 answers

1

The Body of your responseis already your object!

So try it this way:

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());
            }
        });
    }

1


To get the result is Answer.body does like this: User replies service = Answer.body dai tu instance the user class (the POJO you created ) and saves like this: user.setname = answerservice.getname, if it is from a list it is a little different, this is all inside the Answer

Browser other questions tagged

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