Android-Retrofit: getting escaped double-quoted JSON


I'm having problems receiving a JSON response from a REST endpoint using Retrofit. Some of the requests return JSON with a bunch of null characters and escaped double quotes.

Pasted from Android Studio's network inspector:

"{\"type\": \"FeatureCollection\", \"maxSpeed\": \"59.81\", \"minSpeed\": \"0.06\", \"features\": [{\"type\": \"Feature\" ...   

Some requests return bad JSON, while others work flawlessly. For example (sensitive data removed):

my.backend.com/api/getData/111/2026-03-10T10:00:00.000Z -> Correct JSON
my.backend.com/api/getData/111/2026-03-10T12:00:00.000Z -> Bad JSON

This can be seen only from my Android app. Any of those calls, when run from Postman or Curl, return perfectly correct JSON. Also, the backend team has confirmed the API is not at fault.

I'm using the following Retrofit-OkHttp config:

private val tokenInterceptor = Interceptor { chain -\>
  
    val token = runBlocking {
  
        context.dataStore.data
  
            .catch {
  
                emit(emptyPreferences())
  
            }
  
            .first()\[userTokenKey\]
  
    }
  
    val request = chain.request()
  
    val newRequest = if (token == null) request else request.newBuilder()
  
        .header("Authorization", "Bearer $token")
  
        .build()
  
    chain.proceed(newRequest)
  
}
  

  
private val okHttpClient = OkHttpClient.Builder()
  
    .addInterceptor(tokenInterceptor)
  
    .build()
  

  
private val retrofit = Retrofit.Builder()
  
    .addConverterFactory(Json.asConverterFactory("application/json".toMediaType()))
  
    .baseUrl(BuildConfig.SERVER_URL)
  
    .client(okHttpClient)
  
    .build()
2
Mar 12 at 12:05 PM
User AvatarMiguel Vidal
#android#json#retrofit#retrofit2#okhttp

No answer found for this question yet.