我正在尝试使用 Kotlin Coroutines 使用 Moshi Library for JSON Array 进行解析。
代码使用
fun retrofitIndia(baseUrl : String) : Retrofit = Retrofit.Builder()
.client(clientIndia)
.baseUrl(baseUrl)
.addConverterFactory(MoshiConverterFactory.create())
.addCallAdapterFactory(CoroutineCallAdapterFactory())
.build()
我在解析 JSON Array 的数据类时遇到问题。我对 JSON 对象使用了相同的方法,它工作正常,但在数组期间,它崩溃了
下面是崩溃线
java.lang.IllegalArgumentException: Unable to create converter for java.util.ArrayList<data.india.Delta2>
我从 Globallaunch 协程调用失败
代码 :
GlobalScope.launch(Dispatchers.Main) {
val statsRequest = i.getStats()
try {
val response = statsRequest.await()
if(response.){
val statsResponse = response.body() //This is single object Tmdb Movie response
Log.i("stats",""+statsResponse)
}else{
Log.d("MainActivity ",response.errorBody().toString())
}
}catch (e: Exception){
Log.e("Exception",e.localizedMessage)
}
}
最佳答案
您应该将类型设为 List<T>
, Moshi 只支持集合接口(interface),不支持 ArrayList<T>
这样的具体集合类, LinkedList<T>
等。其他类型的集合也是如此:使用 Set<T>
而不是 HashSet<T>
, 和 Map<K, V>
而不是 HashMap<K, V>
, ETC..
关于android - 使用带有 Retrofit 和 Kotlin 的 moshi 将 jsonarray 解析为对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61030259/