Retrofit/Moshi: Platform class java.util.Date requires explicit JsonAdapter to be registered

3k Views Asked by At

I'm new to Android/Retrofit and Moshi. I'm trying to make a POST call to my API and I have a problem with serialising Date. If you see anything else that needs correcting, please point it out as I'm still learning. Thanks

interface ApiInterface {
    @Headers("token: ${Config.API_TOKEN}", "Content-Type: application/json")
    @POST("/api/signup")
    fun signUpNoEmail(@Body userInfo: UserInfo) : Call<SignUpJson>
}

object ApiService {
    private val SERVICE : ApiInterface
    init {
        val retrofit = Retrofit.Builder()
            .baseUrl(Config.BASE_URL)
            .addConverterFactory(MoshiConverterFactory.create())
            .build()
        SERVICE = retrofit.create(ApiInterface::class.java)
    }

    fun signUpNoEmail(userInfo: UserInfo) : Call<SignUpJson> = SERVICE.signUpNoEmail(userInfo)

SignUpJson.kt

@JsonClass(generateAdapter = true)
data class SignUpJson(val vpn_code: String, val expiry_date: Date)
@JsonClass(generateAdapter = true)
data class UserInfo(val username: String)

MainActivity:

val userInfo = UserInfo(username = deviceId)
ApiService.signUpNoEmail(userInfo).enqueue(object : Callback<SignUpJson> {
            override fun onResponse(
                call: Call<SignUpJson>,
                response: Response<SignUpJson>
            ) {
                response.let { serverResponse ->
                    if (serverResponse.isSuccessful) {
                        
                    }
                }
            }

            override fun onFailure(call: Call<SignUpJson>, t: Throwable) {

            }
        })

Caused by: java.lang.IllegalArgumentException: Unable to create converter for class SignUpJson

Caused by: java.lang.IllegalArgumentException: Platform class java.util.Date requires explicit JsonAdapter to be registered

0

There are 0 best solutions below