The line EarthlingsApi.retrofitService.register(params)
in my code below
fun clickLogin(){
val params = HashMap<String, String>()
params["email"] = email
params["idToken"] = idToken
viewModelScope.launch {
try {
val userRegisterResult = EarthlingsApi.retrofitService.register(params)
} catch (e: Exception) {
Timber.d("exception? " + e.toString())
_response.value = "Failure: ${e.message}"
}
}
}
will always return the error exception? java.lang.IllegalArgumentException: Unable to create @Body converter for java.util.HashMap<java.lang.String, java.lang.String> (parameter #1) for method ApiService.register
And below is the code for EarthlingsApi
private val moshi = Moshi.Builder()
.add(KotlinJsonAdapterFactory())
.build()
private val retrofit = Retrofit.Builder()
.addConverterFactory(MoshiConverterFactory.create(moshi))
.baseUrl(BASE_URL)
.build()
interface ApiService {
@POST("/user/register")
suspend fun register(@Body params: HashMap<String, String>?): UserRegister?
}
object EarthlingsApi {
val retrofitService : ApiService by lazy {retrofit.create(ApiService::class.java) }
}
Previously I use Gson and never faced this error. Is there anything else I should do when using Moshi?