Skip to content

Instantly share code, notes, and snippets.

@dron247
Created January 23, 2017 04:28
Show Gist options
  • Save dron247/dbdf83e5b29fee96032625c6bb2fd2fd to your computer and use it in GitHub Desktop.
Save dron247/dbdf83e5b29fee96032625c6bb2fd2fd to your computer and use it in GitHub Desktop.
Okhttp 401 interceptor, handles session expired error
package com.company.product.net.interceptor;
import java.io.IOException;
import okhttp3.Interceptor;
import okhttp3.Request;
import okhttp3.Response;
/**
* Connect this interceptor to okhttp3 line to intercept all 401 answers from service and get callback
*/
public final class SessionExpiredInterceptor implements Interceptor {
public final int INTERCEPT_CODE = 401;
InterceptedRequestHandler listener;
public void setListener(InterceptedRequestHandler listener) {
this.listener = listener;
}
@Override
public Response intercept(Chain chain) throws IOException {
Request request = chain.request();
Response response = chain.proceed(request);
if (response.code() == INTERCEPT_CODE) { // got "session expired" answer, call the handler
if (listener != null) {
listener.onIntercept(request, response);
}
}
return response;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment