spring-bootpostmanretrofitmssql-jdbckotlin-android

POST API response triggering onFailure in Android client despite working in Postman


In a recent project, I developed an Android client using Kotlin and Retrofit to communicate with a microservice built with Spring Boot, which connects to an MS SQL Server database via JDBC. The client sends a token and a bodyRequest as input parameters to a POST API and expects to receive an object composed of two strings (String) and one integer (Int). When testing the API using Postman, I was able to get the expected response, which included two strings and an integer. However, when I implemented the same API call in the Android client, the response never entered the onSuccess block but consistently fell into the onFailure block.

Postman response

{
"codigoRespuesta":"00",
"mensajeRespuesta":"CORRECTO",
"idTransaccion":2371623456182
}

Data class (Kotlin)

data class Response(
@serializedName("codigoRespuesta") val codigoRespuesta: String,
@serializedName("mensajeRespuesta") val mensajeRespuesta: String,
@serializedName("idTransaccion") val idTransaccion: Int
)

Solution

  • After extensive investigation, I discovered that the issue was due to the data type used. In Kotlin, the Int type has a 32-bit limit, which can represent values up to 2,147,483,647 (10 digits). However, the integer received from the server had 13 digits, which exceeded this limit.

    The fix

    I changed the data type from Int to Long (which supports up to 64 bits) in the response class. This solved the issue, and the client started processing the response successfully.