Lance
Lance

Reputation: 2923

Retrofit does not call failure and response.code() is 400

I'm sure there's something wrong with what I'm doing here. Retrofit does not go to failure even if the code that I'm getting is 400.

        AuthenticationService authService = retrofit.create(AuthenticationService.class);
        Call<ValidateTokenResponseMessage> request = authService.validateToken(token);
        request.enqueue(new Callback<ValidateTokenResponseMessage>() {
            @Override
            public void onResponse(Call<ValidateTokenResponseMessage> call, retrofit2.Response<ValidateTokenResponseMessage> response) {
            // When I try to put a breakpoint here I can see that response.code() is 400. 
                ValidateTokenResponseMessage body = response.body();
                if (!body.getValidToken()) {
                    // do success 
                }
            }

            @Override
            public void onFailure(Call<ValidateTokenResponseMessage> call, Throwable t) {
                // do failure
            }
        });

Upvotes: 1

Views: 650

Answers (1)

David Medenjak
David Medenjak

Reputation: 34532

Callback.onFailure only gets called for exceptions like missing internet connection or invalid https etc.

Invoked when a network exception occurred talking to the server or when an unexpected exception occurred creating the request or processing the response.

To check for errors you have to use Response.isSuccessful()

Returns true if code() is in the range [200..300).

Upvotes: 5

Related Questions