0

I'm using Android Mapbox SDK 7.0.0 and I give the SDK an authentication token with the given method: Mapbox.getInstance(applicationContext, mapboxToken)

My authentication token, mapboxToken, is working fine and the map is displayed.

But, how to detect when the authentication token expires?

I want to refresh this authentication token from time to time, but this also means that the Android app has to detect expired tokens in order to ask for a new one.

If no tile is in cache, then the MapboxView triggers the OnDidFailLoadingMapListener but if there are some tiles in cache, the map is loaded correctly, new tiles are not loaded and no callback is triggered.

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Antoine44
  • 11
  • 3

1 Answers1

0

I found a solution to my problem. It's possible to override the Http client of the Mapbox SDK. This workaround works for the Mapbox SDK provided for Android. Adding an interceptor we can interprete the Http code before returning the response to the Mapbox native library.

Here is my workaround to catch unauthorized HTTP codes:

val client = OkHttpClient.Builder().addInterceptor { chain ->
  val response = chain.proceed(chain.request())

  if (response.code() == 401) {
    // Code that handles unauthorized Mapbox token
  }

  response
}.build()

HttpRequestImpl.setOkHttpClient(client)
Antoine44
  • 11
  • 3