Created
January 23, 2017 04:28
-
-
Save dron247/dbdf83e5b29fee96032625c6bb2fd2fd to your computer and use it in GitHub Desktop.
Okhttp 401 interceptor, handles session expired error
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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