OkHttp automatically refreshes global expired tokens and okhttptoken
# Problems:
The currently developed App encounters a problem:
When you request an interface, the interface reports an error because the token has expired.
However, the product manager wants the app to refresh the token immediately and then repeat the request to the interface. This process is imperceptible to users.
>
That is, silent automatic login, and then continue the request:
>
Request A interface-> the server returns the token expiration-> request the token refresh interface-> request A Interface
>
How can we achieve the above requirements?
# Solution:
Ideas:
1. Get the returned data through the interceptor
2. Determine whether the token has expired
3. If the token expires, refresh the token.
4. Use the latest token to request network data again
1/** 2 * automatically refresh Token Interceptor 3 */4 public class TokenInterceptor implements Interceptor {5 6 @ Override 7 public Response intercept (Chain chain Chain) throws IOException {8 Request request = chain. request (); 9 Response response = chain. proceed (request); 10 LogUtil. print ("response. code = "+ response. code (); 11 12 if (isTokenExpired (response) {// judge token expiration 13 LogUtil according to the agreement with the server. print ("Silently refresh the Token automatically and then request data again"); 14 // synchronous request method, get the latest Token15 String newSession = getNewToken (); 16 // use the new Token to create a new Request 17 Request newRequest = chain. request () 18. newBuilder () 19. header ("Cookie", "JSESSIONID =" + newSession) 20. build (); 21 // request 22 return chain again. proceed (newRequest); 23} 24 return response; 25} 26 27/** 28 * According to Response, determine whether the Token is invalid 29*30 * @ param response31 * @ return32 */33 private boolean isTokenExpired (Response response) {34 if (response. code () = 404) {35 return true; 36} 37 return false; 38} 39 40/** 41 * synchronous request method, get the latest Token42 * 43 * @ return44 */45 private String getNewToken () throws IOException {46 // get the new token through a specific interface, the synchronized merge fit request 47 Response_Login loginInfo = CacheManager is used here. restoreLoginInfo (BaseApplication. getContext (); 48 String username = loginInfo. getUserName (); 49 String password = loginInfo. getPassword (); 50 51 LogUtil. print ("loginInfo =" + loginInfo. toString (); 52 Call <Response_Login> call = WebHelper. getSyncInterface (). synclogin (new Request_Login (username, password); 53 loginInfo = call.exe cute (). body (); 54 LogUtil. print ("loginInfo =" + loginInfo. toString (); 55 56 loginInfo. setPassword (password); 57 CacheManager. saveLoginInfo (loginInfo); 58 return loginInfo. getSession (); 59} 60}
Then configure OkHttp
HttpLoggingInterceptor logging = new HttpLoggingInterceptor(); logging.setLevel(HttpLoggingInterceptor.Level.BODY); OkHttpClient client = new OkHttpClient.Builder() .connectTimeout(15, TimeUnit.SECONDS) .readTimeout(300, TimeUnit.SECONDS) .writeTimeout(300, TimeUnit.SECONDS) .cache(new Cache(FileConstants.HTTP_CACHE_DIR, FileConstants.CACHE_SIZE)) .addInterceptor(interceptor)// .addInterceptor(new MockInterceptor()) .addInterceptor(new TokenInterceptor())// .addInterceptor(new RetryIntercepter(3)) .addInterceptor(logging) .build();