5

I am trying to parse using Moshi Library for JSON Array using Kotlin Coroutines .

Code use

 fun retrofitIndia(baseUrl : String) : Retrofit = Retrofit.Builder()
        .client(clientIndia)
        .baseUrl(baseUrl)
        .addConverterFactory(MoshiConverterFactory.create())
        .addCallAdapterFactory(CoroutineCallAdapterFactory())
        .build()

I get issue while Parsing the data class for JSON Array . I have used same for JSON Object and it works fine but during array , it crashes Below is the crash line

java.lang.IllegalArgumentException: Unable to create converter for java.util.ArrayList<data.india.Delta2>

I call from Globallaunch coroutine where it gets failed

Code :

 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)
            }
        }
Yatin
  • 2,969
  • 9
  • 34
  • 68
  • Could you provide from which line the error is thrown? or show full stacktrace? – Animesh Sahu Apr 04 '20 at 16:35
  • I have a jsonArray as a json output to be consumed ... My error gets here when I call it from Kotlin Coroutine GlobalScope.launch(Dispatchers.Main) { val statsRequest = iservice.getStats() try { val response = statsRequest.await() if(response.){ val statsResponse = response.body() //This is single object Tmdb Movie response } } – Yatin Apr 04 '20 at 16:57
  • @AnimeshSahu : Any idea of parsing JSONArray from Moshi ? – Yatin Apr 07 '20 at 06:31
  • Don't know man, didn't worked with those libs, bounty the question maybe, take +1. – Animesh Sahu Apr 07 '20 at 07:37
  • 3
    What is your JSON response format (example json response) and parser class.? Also full stacktrace would be helpful.. – Nataraj KR Apr 07 '20 at 09:45

2 Answers2

3

You should make the type just List<T>, Moshi only supports the collection interfaces, not the concrete collection classes like ArrayList<T>, LinkedList<T>, etc.. The same goes for other kinds of collections: use Set<T> instead of HashSet<T>, and Map<K, V> instead of HashMap<K, V>, etc..

Angus H
  • 404
  • 3
  • 8
2

I don't think the coroutines have anything with the parsing error, try following Reading Json lists using Moshi

Quick snippet will look something like:

// Single item declaration 
class SingleListItem(val title: String, val number: Int)

private var listMyData = Types.newParameterizedType(MutableList::class.java, SingleListItem::class.java)
private val adapter: JsonAdapter<List<SingleListItem>> = Moshi.Builder().add(KotlinJsonAdapterFactory()).build().adapter(listMyData)
Sean
  • 5,176
  • 2
  • 34
  • 50