Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Expected BEGIN_ARRAY but was BEGIN_OBJECT at line 1 column 2 retrofit2

I have problem with my project. It is simple connection with api application. I want to take information like this:

{
    "docs": [
        {
            "_id": "5cf5805fb53e011a64671582",
            "name": "The Fellowship Of The Ring"
        },
        {
            "_id": "5cf58077b53e011a64671583",
            "name": "The Two Towers"
        },
        {
            "_id": "5cf58080b53e011a64671584",
            "name": "The Return Of The King"
        }
    ],
    "total": 3,
    "limit": 1000,
    "offset": 0,
    "page": 1,
    "pages": 1
}

But I got illegalStateException like in title. Theres is my files:

DTO

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

data class CharacterDTO(
    val docs: List<CharacterData>,
    val limit: Int,
    val offset: Int,
    val page: Int,
    val pages: Int,
    val total: Int
)
data class CharacterData(
    val _id: String,
    val birth: String,
    val death: String,
    val gender: String,
    val hair: String,
    val height: String,
    val name: String,
    val race: String,
    val realm: String,
    val spouse: String,
    val wikiUrl: String
)

fun CharacterData.toCharacter(): Character{
    return Character(
        _id = _id,
        birth = birth,
        death = death,
        gender = gender,
        name = name,
        race = race
    )
}

And it’s crash on this file:
USECASE

lass GetCharactersUseCase @Inject constructor(
    private val repository: LotrRepository
) {
    operator fun invoke(): Flow<List<Character>> = flow {
        try {
            val characters = repository.getCharacters() // on this line it crash
            Log.d(Tag,characters.toString())
        }catch (e: HttpException){
            Log.d(Tag,e.toString())
        }catch (e: IOException){
            Log.d(Tag,e.toString())
        }
    }
}

And this is repository

interface LotrRepository {

    suspend fun getBooks(): List<BookDTO>

    suspend fun getCharacters(): List<CharacterDTO>

    suspend fun getCharacterById(characterId: String): CharacterDTO
}

And last this is my request:

    @Provides
    @Singleton
    fun provideLotrApi(): LotrApi{
        val request = Retrofit.Builder()
            .client(OkHttpClient.Builder().addInterceptor { chain ->
                val request = chain.request().newBuilder().addHeader("Authorization", "Bearer ${Constants.API_KEY}").build()
                chain.proceed(request)
            }.build())
            .baseUrl(Constants.BASE_URL)
            .addConverterFactory(GsonConverterFactory.create())
            .build()
            .create(LotrApi::class.java)
        return request
    }

After my research i forgot to handle with this part

    "total": 3,
    "limit": 1000,
    "offset": 0,
    "page": 1,
    "pages": 1

But after changes my DTO file it’s still don’t work

>Solution :

JSON provided above contains only a single CharacterDTO object, but you expected to get a list of such objects. Either change the backend to return a list or replace getCharacters() declaration with:

suspend fun getCharacters(): CharacterDTO
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading