Here's my code
interface ApiService {
@POST("sms-otp/request")
suspend fun loginTest(@Body testRequest: TestRequest): Response<TestResponse>
}
data class TestResponse(
@SerializedName("otp")
@Expose
var otp : String? = null,
@Expose
@SerializedName("otpId")
var otpId: Int? = null
)
Logic -
loginRepository.loginTest(testRequest.phoneNumber!!).let {
if (it.isSuccessful) {
_res.postValue(Resource.success(it.body()))
Log.d("_res", it.body().toString())
Log.d("_res" , _res.value.toString())
Log.d("_res", _res.value?.data.toString())
} else {
_res.postValue(Resource.error(it.errorBody().toString(), null))
}
}
Here is my Log , I think response code is 201, maybe success but no data in my model... please help me
You are getting a response from the server, as okhttp
logging is showing. But, retrofit is not able to map it. As you have the wrong kotlin data class for the response.
{
"code": "0000",
"message": "ok",
"data": {
"otpId": 21,
"otp": "518793"
}
}
Response has otpData
inside data
object, but your TestResponse
only contains otp
properties.
For retrofit to map correctly, you have to define two data classes, one for the parent response and one for the otpData
Change TestResponse.kt
to this
data class TestResponse(
@SerializedName("code")
@Expose
val code: String,
@SerializedName("data")
@Expose
val otpData: OtpData,
@SerializedName("message")
@Expose
val message: String
)
Now, createOtpData.kt
data class OtpData(
@SerializedName("otp")
@Expose
val otp: String,
@SerializedName("otpId")
@Expose
val otpId: Int
)