0

here are the nested JSON request

{
"user": {
    "email": "[email protected]",
    "password": "12345678"
    }
}

here are the nested JSON response

{
    "data": {
        "renew_token": "e994c4d2-d93b-47e8-ab5f-9090b823f249",
        "token": "419fff70-b1ee-4ea7-b636-ddbec6346794"
    }
}

i am able to post the request but i'm struggling to code the response in the same process

my interface currently

public interface JsonApi {

    @POST("session")
    Call<RootUser> userLogin(@Body RootUser rootUser);
}

Model for request

public class User{
    public String email;
    public String password;
}

public class RootUser{
    public User user;
}

the API call

private void userLogin(){
        String email = etloginemail.getText().toString();
        String password = etloginpassword.getText().toString();

        Retrofit retrofit = new Retrofit.Builder().baseUrl("https://the-digest-app.herokuapp.com/api/")
                                        .addConverterFactory(GsonConverterFactory.create())
                                        .build();

        JsonApi jsonApi = retrofit.create(JsonApi.class);

        User user = new User(email, password);
        RootUser rootUser = new RootUser(user);

        Call<RootUser> call = jsonApi.userLogin(rootUser);

        call.enqueue(new Callback<RootUser>() {
            @Override
            public void onResponse(Call<RootUser> call, Response<RootUser> response) {
                Toast.makeText(LoginActivity.this, "Success", Toast.LENGTH_LONG).show();
                         }

            @Override
            public void onFailure(Call<RootUser> call, Throwable t) {
                Toast.makeText(LoginActivity.this, "Error", Toast.LENGTH_LONG).show();
            }
        });

    }

How to define the get response function? if i did something wrong, kindly tell me!

1
  • Your request and response are different! You will need to different POJOs like @mehmet said. How do we know it? Both the request and response contain two keys of type string but both have different key names! Commented Dec 1, 2020 at 15:59

1 Answer 1

1

You should create one more POJO for response like

public class LoginResponse{
    @SerializedName("renew_token")
    public String renewToken;
    @SerializedName("token")
    public String token;
}

public class UserLoginResponse{
    public LoginResponse data;
}

and your retrofit interface should look like

public interface JsonApi {

    @POST("session")
    Call<UserLoginResponse> userLogin(@Body RootUser rootUser);
}

and your call implementation

call.enqueue(new Callback<UserLoginResponse>() {
            @Override
            public void onResponse(Call<UserLoginResponse> call, Response<UserLoginResponse> response) {
                Toast.makeText(LoginActivity.this, "Success", Toast.LENGTH_LONG).show();
                         }

            @Override
            public void onFailure(Call<UserLoginResponse> call, Throwable t) {
                Toast.makeText(LoginActivity.this, "Error", Toast.LENGTH_LONG).show();
            }
        });
Sign up to request clarification or add additional context in comments.

4 Comments

I have created the POJO and edit the interface, can you help me with calling the get response? im struggling because its in nested json format.
I edited my answer and add implementation.
Thanks for your help! sorry if my questions aren't clear enough but i am actually asking for the code for getting the response "token" and "renew_token" to store inside the class LoginResponse. If u can help that would be much appreciated!
Never mind i already figured it out. Wouldn't be possible without your help though :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.