Retrofit/Moshi:平台类 java.util.Date 需要显式注册 JsonAdapter

Hou*_*man 5 android kotlin retrofit moshi

我是 Android/Retrofit 和 Moshi 的新手。我正在尝试对我的 API 进行 POST 调用,但在序列化方面遇到问题Date。如果您发现任何其他需要纠正的地方,请指出,因为我仍在学习。谢谢

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)
Run Code Online (Sandbox Code Playgroud)

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)
Run Code Online (Sandbox Code Playgroud)

主要活动:

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) {

            }
        })
Run Code Online (Sandbox Code Playgroud)

引起原因:java.lang.IllegalArgumentException:无法为类 SignUpJson 创建转换器

引起原因:java.lang.IllegalArgumentException:平台类 java.util.Date 需要显式注册 JsonAdapter