Home > Mobile >  Expected BEGIN_ARRAY but was BEGIN_OBJECT at path $ for Andoid
Expected BEGIN_ARRAY but was BEGIN_OBJECT at path $ for Andoid

Time:08-22

I am looking to call this api, but it seems to be in an object call.

{"Abstract":"","AbstractSource":"Wikipedia","AbstractText":"","AbstractURL":"https://en.wikipedia.org/wiki/The_Wire_characters","Answer":"","AnswerType":"","Definition":"","DefinitionSource":"","DefinitionURL":"","Entity":"","Heading":"The Wire characters","Image":"","ImageHeight":0,"ImageIsLogo":0,"ImageWidth":0,"Infobox":"","Redirect":"","RelatedTopics":[{"FirstURL":"https://duckduckgo.com/Alma_Gutierrez","Icon":{"Height":"","URL":"/i/8690ca01.jpg","Width":""},"Result":"<a href="https://duckduckgo.com/Alma_Gutierrez">Alma Gutierrez
Alma M. Gutierrez is a fictional character on the HBO drama The Wire, played by actress Michelle Paress. Gutierrez is a dedicated and idealistic young reporter on the city desk of The Baltimore Sun.","Text":"Alma Gutierrez - Alma M. Gutierrez is a fictional character on the HBO drama The Wire, played by actress Michelle Paress. Gutierrez is a dedicated and idealistic young reporter on the city desk of The Baltimore Sun."},{"FirstURL":"https://duckduckgo.com/Augustus_Haynes","Icon":{"Height":"","URL":"/i/872d317a.jpg","Width":""},"Result":"<a href="https://duckduckgo.com/Augustus_Haynes">Augustus Haynes ...}

As you can see, the call is an object call, which is resulting to me having me get the error...

Expected BEGIN_ARRAY but was BEGIN_OBJECT at path $

How would you call the object instead of calling a list?

here's the Interface:

@Singleton
interface AnywhereAPI {
    @GET(".")
    suspend fun getAnyInfo(
        @Query("q") query : String = "the wire characters&format=json",
        @Query("format") format : String = "json"
    ): Response<List<GetAnyResponse>>
}

The repository:

class AnywhereRepository @Inject constructor(
    private val api: AnywhereAPI,
    private val anywhereDao: AnywhereDao
){
//  private val dataOrException = DataOrException<List<AnywhereListEntity>, Boolean, Exception>()
//
//    suspend fun getAllInfos(): DataOrException<List<AnywhereListEntity>, Boolean, Exception>{
//        try {
//            dataOrException.loading = true
//            dataOrException.data = api.getAnyInfo()
//        }
//    }

        val feeds: Flow<List<AnywhereListEntity>>
            get() = anywhereDao.getInfo()


    suspend fun anywhereInfo(): List<AnywhereListEntity>? {
        val request = api.getAnyInfo()
        if (request.isSuccessful){
            val anyItems = request.body()!!.map {
                AnywhereMapper.buildFrom(it)
            }
            anywhereDao.insertInfo(*anyItems.toTypedArray())
            return anyItems
        }
        return null
    }
}

the View Model:

class AnyViewModel @Inject constructor(
    private val repository: AnywhereRepository
): ViewModel() {
   val anyInfoResults = repository.feeds

    init {
        getAnyList()
    }

    private fun getAnyList() {
        viewModelScope.launch {
            try {
               repository.anywhereInfo()!!
            }catch (e: Exception){
                Log.e("REPO", e.message, e.cause)
        }

        }
    }
}

I'll leave a link to my project, so you guys can take a look at it. Thank you for the help GitHub project https://github.com/OEThe11/AnywhereCE

Also, the API URL: http://api.duckduckgo.com/?q=the wire characters&format=json

CodePudding user response:

You are trying to parse a List but you get a JSON object:

Expected BEGIN_ARRAY but was BEGIN_OBJECT at path $

Try to change return type in Retrofit interface from Response<List<GetAnyResponse>> to Response<GetAnyResponse>.

  • Related