Skip to content Skip to sidebar Skip to footer

Retrofit Handling Token Expiration

Right now im using Retrofit to fitch data from server. the problem is i need to call API to get new token every 1 hour, and so far im running background service to get new token be

Solution 1:

Retrofit is made to help YOU handle the requests you need. It doesn't handle token expiration. This means, that you need to handle that yourself. For example, in your request error handling, you can check for 401 error Unauthorized (this is usually the error a server sends when a token expires). When this occurs, hold the failed request and send another request for a new token. When the new request is successfully completed, you will have your token refreshed and you can retry the initial failed request with the newly fetched token.

Solution 2:

i think that u can use an OkHttp Interceptor to catch 401 Unauthorized for any request and than refresh your token and retry your request.

https://futurestud.io/tutorials/retrofit-2-catch-server-errors-globally-with-response-interceptor

Global Error Handler: OkHttp Interceptor

Post a Comment for "Retrofit Handling Token Expiration"