CodexBloom - Programming Q&A Platform

Unexpected behavior with Retrofit response parsing using GSON in Android 14

πŸ‘€ Views: 431 πŸ’¬ Answers: 1 πŸ“… Created: 2025-09-06
Android Retrofit GSON JSON Coroutines Kotlin

This might be a silly question, but I'm facing an issue with parsing a JSON response from a Retrofit call using GSON in my Android app targeting API level 14. The JSON structure seems valid, but I'm receiving a `JsonSyntaxException` during the deserialization process. The response looks like this: ```json { "user": { "id": 123, "name": "John Doe", "email": "john.doe@example.com" }, "status": "success" } ``` I defined the following data classes: ```kotlin data class User( val id: Int, val name: String, val email: String ) data class ApiResponse( val user: User, val status: String ) ``` Here’s how I set up Retrofit: ```kotlin val retrofit = Retrofit.Builder() .baseUrl("https://api.example.com/") .addConverterFactory(GsonConverterFactory.create()) .build() val service = retrofit.create(ApiService::class.java) ``` And the interface for the API call: ```kotlin interface ApiService { @GET("user/details") suspend fun getUserDetails(): ApiResponse } ``` When I call `getUserDetails()`, I receive the following exception: ```plaintext com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was STRING at line 1 column 1 ``` I've checked the network response using a debugger and confirmed that the data matches the structure of my data classes. I've also tried using `@SerializedName` annotations to ensure property mapping, but the issue persists. Could this be related to the response being wrapped in a different format or encoding? Any guidance on how to resolve this would be greatly appreciated. For context: I'm using Kotlin on Linux.