How to parse Json with retrofit

0

Webservice response

{
    "estado": 400,
    "mensaje": "Usuario o Password invalidos"
}

I am starting to make use of retrofit, there are some parts that I do not understand, it is necessary to say that it is the first time I use it.

Create my model based on those two response parameters that I have in this case  I'll just need a message

@SerializedName("mensaje")
private String mensaje;
@SerializedName("username")
private String username;
@SerializedName("email")
private String email;
@SerializedName("password")
private String password;

CONSTRUCT (email, password) ...
GET AND SET ...

Using retrofit.

Call<LoginModel> call = MainActivity.apiInterface.loginUser(loginModel);
        call.enqueue(new Callback<LoginModel>() {
            @Override
            public void onResponse(Call<LoginModel> call, Response<LoginModel> response) {
                if (response.isSuccessful()){


                } else{
                    // errores 400...etc
                }

            }

            @Override
            public void onFailure(Call<LoginModel> call, Throwable t) {

            }
        });

How it should be used, that is to say how I get "message" from my api and show it by console so as not to make it long. I can make the request correctly, but I do not know how to parse the data, try to use the following in my answer, but it throws me an error. What am I missing?

String message = response.body().getMensaje();
Error: java.lang.NullPointerException
    
asked by DoubleM 13.08.2018 в 00:26
source

0 answers