Skip to content

Instantly share code, notes, and snippets.

@WrathChaos
Created December 14, 2019 16:10
Show Gist options
  • Save WrathChaos/12faa0082a6195581a5b3aa87a5ac069 to your computer and use it in GitHub Desktop.
Save WrathChaos/12faa0082a6195581a5b3aa87a5ac069 to your computer and use it in GitHub Desktop.
How to handle 401 error on RetroFit
// Add other Interceptors
httpClient.addInterceptor(new Interceptor() {
    @Override
    public Response intercept(Interceptor.Chain chain) throws IOException {
        Request original = chain.request();
        Request request = original.newBuilder()
                .header("Authorization", token_type + " " + access_token)
                .method(original.method(), original.body())
                .build();

        Response response =  chain.proceed(request);
        Log.d("MyApp", "Code : "+response.code());
        if (response.code() == 401){
            // Magic is here ( Handle the error as your way )
            return response;
        }
        return response;
    }
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment