android – Why retrofit response.body() return null if the logger display the object from the body?-ThrowExceptions

Exception or error:

I’m trying to verify the email of an user before registering him and I have the following java endpoint that will be called from an android app:

@PostMapping("/user/register")
    public ResponseEntity<UserResponse> register(@RequestBody UserDTO userDTO){
        try {
            User user = userService.createUser(userDTO);
            if (user != null) {
                UserResponse userResponse = new UserResponse(user,"success");
                return new ResponseEntity<UserResponse>(userResponse, HttpStatus.OK);
            }
            return new ResponseEntity<>(HttpStatus.NOT_FOUND);
        } catch (EmailHasBeenTakenException emailHasBeenTakenException) {
            UserResponse userResponse = new UserResponse(null,emailHasBeenTakenException.getMessage());
            return new ResponseEntity<UserResponse>(userResponse, HttpStatus.BAD_REQUEST);
        }catch (UsernameHasBeenTakenException usernameHasBeenTakenException) {
            UserResponse userResponse = new UserResponse(null,usernameHasBeenTakenException.getMessage());
            return new ResponseEntity<UserResponse>(userResponse, HttpStatus.BAD_REQUEST);
        }
    }

Now when I’m trying to register with an existing email I want to get the response body and to display the message information.
Retrofit request:

UserClient client = retrofit.create(UserClient.class);
        Call<UserResponse> call = client.createAccount(user);

        call.enqueue(new Callback<UserResponse> () {
            @Override
            public void onResponse(Call<UserResponse> call, Response<UserResponse> response) {
//                JsonObject post = new JsonObject().get(response.body().toString()).getAsJsonObject();
                System.out.println("Response is: "+ response.body());
                Toast.makeText(RegistrationActivity.this, "Id is: " + response.body(), Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onFailure(Call<UserResponse> call, Throwable t) {
                Toast.makeText(RegistrationActivity.this, "something went wrong", Toast.LENGTH_SHORT).show();
            }
        });

UserResponse POJO is:

public class UserResponse {
    private User user;
    private String message;

    public User getUser() {
        return user;
    }

    public void setUser(User user) {
        this.user = user;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }
}

User class:

public class User {

    private Long id;

    private String firstName;
    private String lastName;
    private String email;
    private String username;
    private String password;

    public User (String firstName, String lastName, String email, String username, String password) {
        this.firstName = firstName;
        this.lastName = lastName;
        this.email = email;
        this.username = username;
        this.password = password;
    }

    public Long getId () {
        return id;
    }
}

When the request is made I’m getting the following response log:

D/OkHttp: <-- 400 http://my_ip:8080/user/register/ (80ms)
    Content-Type: application/json
    Transfer-Encoding: chunked
    Date: Sun, 23 Feb 2020 15:59:01 GMT
    Connection: close
D/OkHttp: {"user":null,"message":"Email abc@gmail.com has been taken!"}
    <-- END HTTP (68-byte body)
I/System.out: Response is: null

Why the response body is null?

How to solve:

Solved. Because of status code 400 I have to use response.errorBody() to get the response.

Leave a Reply

Your email address will not be published. Required fields are marked *