android - 使用带有 Retrofit 和 Kotlin 的 moshi 将 jsonarray 解析为对象

标签 android arrays json kotlin moshi

我正在尝试使用 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/

相关文章:

android - 解析 xml 文件

android - 如何使用 Java 在 Android Wi-Fi 连接上设置 ProxySettings 和 ProxyProperties?

android - flutter :可通过卡和 margin 关闭

arrays - 修改作为映射值的数组

javascript - Typeahead 结果填充返回整个数组对象

php - 谷歌地图 : org. json.JSONArray 无法转换为 json 对象

android - 如何让EditText滚动?

c - 如何在排序之前获取数组中数字的前一个位置?

iphone - 使用 AFJSONRequestOperation

javascript - EXTJS:如何从 JSON 文件加载 Ext.form.panel 项目配置