Retrofit2 onFailure did not cause error 401

I make a network call to enter the service, and if you pass an invalid login, the Call object from retrofit2 still calls onResponse, but never onFailure.

Even in onResponseI can call response.code(), and the return code is 401, as expected.

Any idea what I'm doing wrong?

Here is my code for activity that runs everything:

Call<Login> call = LoginUtils.loginUser(ServiceUtils.getRequestHeaders(LoginActivity.this),
        BuildConfig.ORDER_SERVICE_DOMAIN, uname, pass);
call.enqueue(new retrofit2.Callback<Login>() {
    @Override
    public void onResponse(Call<Login> call, retrofit2.Response<Login> response) {
        System.out.println("onResponse");
        System.out.println("responseCode = " + response.code());
    }

    @Override
    public void onFailure(Call<Login> call, Throwable t) {
        System.out.println("onFailure");

    }
});

Here we set the level of service:

public LoginService(final String domain, final Map<String, String> headers) {

    HttpLoggingInterceptor loggingInterceptor = new HttpLoggingInterceptor();
    loggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);

    OkHttpClient httpClient = new OkHttpClient.Builder()
            .connectTimeout(CONNECT_TIMEOUT_SECS, TimeUnit.SECONDS)
            .writeTimeout(READ_TIMEOUT_SECS, TimeUnit.SECONDS)
            .readTimeout(READ_TIMEOUT_SECS, TimeUnit.SECONDS)
            .addInterceptor(new Interceptor() {
                @Override
                public Response intercept(Chain chain) throws IOException {
                    Request original = chain.request();
                    Request.Builder request = original.newBuilder();
                    if (!headers.isEmpty()) {
                        for (Map.Entry<String, String> entry : headers.entrySet()) {
                            request.addHeader(entry.getKey(), entry.getValue());
                        }
                    }
                    request.method(original.method(), original.body());
                    return chain.proceed(request.build());
                }
            })
            .addInterceptor(loggingInterceptor)
            .build();

    // create the rest adapter
    Retrofit retrofit = new Retrofit.Builder()
            .addConverterFactory(GsonConverterFactory.create(GSON))
            .baseUrl(domain)
            .client(httpClient)
            .build();

    loginServices = retrofit.create(LoginServices.class);
}

And finally, our interface:

@GET
Call<Login> getLoginCustompath(@Url String url,
        @Header("Authorization") String authorization);

What I do not see here?

Thank you for your help.

+4
source share
2 answers

401 codemeans " Unautorized", there is no error in the message, and therefore was onFaliurenot called.

, .

, .

+2

Retrofit2:

/**
   * Invoked when a network exception occurred talking to the server or when an unexpected
   * exception occurred creating the request or processing the response.
   */
  void onFailure(Call<T> call, Throwable t);

HTTP 401 Unauthorized, .

: Retrofit2

+2

All Articles