Home > Software engineering >  Expected BEGIN_OBJECT but was BEGIN_ARRAY kotiln
Expected BEGIN_OBJECT but was BEGIN_ARRAY kotiln

Time:01-14

Json reader Expected data to be object but comes as array I want to add offline caching to my images app
when I run the app I get this error and the app crashed

2023-01-12 11:51:31.119 5622-5622/com.example.images E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.images, PID: 5622
    com.squareup.moshi.JsonDataException: Expected BEGIN_OBJECT but was BEGIN_ARRAY at path $
        at com.squareup.moshi.JsonUtf8Reader.beginObject(JsonUtf8Reader.java:172)
        at com.example.images.network.NetworkImageContainerJsonAdapter.fromJson(NetworkImageContainerJsonAdapter.kt:36)
        at com.example.images.network.NetworkImageContainerJsonAdapter.fromJson(NetworkImageContainerJsonAdapter.kt:22)
        at com.squareup.moshi.internal.NullSafeJsonAdapter.fromJson(NullSafeJsonAdapter.java:41)
        at retrofit2.converter.moshi.MoshiResponseBodyConverter.convert(MoshiResponseBodyConverter.java:46)
        at retrofit2.converter.moshi.MoshiResponseBodyConverter.convert(MoshiResponseBodyConverter.java:27)
        at retrofit2.OkHttpCall.parseResponse(OkHttpCall.java:243)
        at retrofit2.OkHttpCall$1.onResponse(OkHttpCall.java:153)
        at okhttp3.internal.connection.RealCall$AsyncCall.run(RealCall.kt:519)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1137)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:637)
        at java.lang.Thread.run(Thread.java:1012)
        Suppressed: kotlinx.coroutines.DiagnosticCoroutineContextException: [StandaloneCoroutine{Cancelling}@db9bb9a, Dispatchers.Main.immediate]

trying to get json data to NetworkImageContainer class

json reader

import com.squareup.moshi.Moshi
import com.squareup.moshi.kotlin.reflect.KotlinJsonAdapterFactory
import retrofit2.Retrofit
import retrofit2.converter.moshi.MoshiConverterFactory
import retrofit2.http.GET


private const val BASE_URL = "https://picsum.photos/"

private val moshi = Moshi.Builder()
    .add(KotlinJsonAdapterFactory())
    .build()

private val retrofit = Retrofit.Builder()
    .addConverterFactory(MoshiConverterFactory.create(moshi))
    .baseUrl(BASE_URL)
    .build()


interface ImageApiService {
    @GET("v2/list")
    suspend fun getImages(): NetworkImageContainer
}

object ImageApiNet {

    val retrofitService= retrofit.create(ImageApiService::class.java)
}

json class and NetworkImageContainer class

import com.example.images.database.DatabaseImages
import com.example.images.domain.ImagesModels
import com.squareup.moshi.Json
import com.squareup.moshi.JsonClass


@JsonClass(generateAdapter = true)
data class NetworkImageContainer(val images:List<NetworkImage>)

@JsonClass(generateAdapter = true)
data class NetworkImage(
    val id: String,
    val author:String,
    @Json(name = "download_url")val download_url: String,
    // val width: String,
    // val height: String,
    // val url: String
    )


fun NetworkImageContainer.asDomainModel(): List<ImagesModels> {
    return images.map {
        ImagesModels(
            id = it.id,
            author = it.author,
            download_url = it.download_url,
       //     width = it.width,
         //   height = it.height,
           // url = it.url
    )
    }
}

fun NetworkImageContainer.asDatabaseModel(): List<DatabaseImages>{
    return images.map {
        DatabaseImages(
            id = it.id,
            author = it.author,
            download_url = it.download_url,
//            width = it.width,
//            height = it.height,
//            url = it.url
        )

    }
}

this is the API I use https://picsum.photos/

[
    {
        "id": "0",
        "author": "Alejandro Escamilla",
        "width": 5616,
        "height": 3744,
        "url": "https://unsplash.com/...",
        "download_url": "https://picsum.photos/..."
    }
]

CodePudding user response:

You are basically telling it that your response looks like

{
    "images": [{
        "id": "0",
        "author": "Alejandro Escamilla",
        "width": 5616,
        "height": 3744,
        "url": "https://unsplash.com/...",
        "download_url": "https://picsum.photos/..."
    }]
}

You just need to get rid of NetworkImageContainer and change

suspend fun getImages(): NetworkImageContainer

to

suspend fun getImages(): List<NetworkImage>
  • Related