In Android with Kotlin and Volley Library, I am using the below code to put a map into jsonObject.
val params = mutableMapOf<String, String>()
params["key1"] = "value1"
params["key2"] = "value2"
val parameter = JSONObject(params)
This give me the below error:
Java type mismatch expected (Mutable)Map<(raw) Any?, (raw) Any?>! but found MutableMap<String, String>. Use explicit cast
params as Map<*, *>The JSONObject constructor takes a Map with raw types (the generics are not specified). As such you should supply a map that can be of Any type :
val params = mutableMapOf<Any?, Any?>()
params["key1"] = "value1"
params["key2"] = "value2"
val parameter = JSONObject(params)
The warning (Mutable)Map<(raw) Any?, (raw) Any?> is showing the Java parameter argument - stating it can expect either a Map or MutableMap, as you are calling Java code from Kotlin - Java won't know if the Map is mutable or immutable. Just to note raw types are not supported by Kotlin, you only encounter this problem if calling Java code.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With