nimondo
nimondo

Reputation: 67

Expected BEGIN_OBJECT but was BEGIN_ARRAY at path Moshi Json Data Exception

I'm using Moshi as converter for Retrofit, but for one particular request it doesn't work and exception is thrown:

com.squareup.moshi.JsonDataException: Expected BEGIN_OBJECT but was BEGIN_ARRAY at path $.animaux

Here is the Json

    "nb_animaux": 1,
    "animaux": [
        {
            "code_iso": "768010103010004",
            "photos": [],
            "espece": {
                "id": 1,
                "fr": "Bovin",
                "en": "Bovine"
            },
            "elevage": {
                "parametre_geographique": {
                    "prefecture": "Oti",
                    "region": "Savane",
                    "commune": "Mango",
                    "pays": {
                        "fr": "TOGO",
                        "en": "TOGO"
                    },
                    "village": "Fonboro"
                },
                "type_elevage": {},
                "code_elevage": "76801010301"
            },
            "sexe": "m",
            "date_naissance": 1561932000,
            "category": {
                "fr": "Taureau",
                "en": "Bull"
            },
            "proprietaire": {
                "id": 10,
                "fullname": "edem",
                "adresse": "lome",
                "telephone": "98665478"
            }
        }
    ],
    "num_transport": "1/11/2019/TG",
    "date_transport": 1574784132,
    "traite": false
}

My model class

@Entity
data class Animal(
    @field:PrimaryKey
    var id: Long,
    @ColumnInfo(name = "nb_animaux")
    @Json(name = "nb_animaux")
    var nbAnimaux : Int,
    @Embedded
    @get:Json(name = "animaux")
    var animaux : Animaux,
    @ColumnInfo(name = "num_transport")
    @Json(name = "num_transport")
    var numeroTransport : String,
    @ColumnInfo(name = "date_transport")
    @Json(name = "date_transport")
    var dateTransport : Long,
    var traite: Boolean

)
@JsonClass(generateAdapter = true)
data class Animaux(
    @ColumnInfo(name = "animaux_id")
    var id : Long,
    @ColumnInfo(name = "code_iso")
    @Json(name = "code_iso")
    var codeIso : Long,
    @Embedded
    var espece : Espece,
    @Embedded
    var elevage : Elevage,
    var sexe : String,
    @ColumnInfo(name = "date_naisance")
    @Json(name = "date_naisance")
    var dateNaissance : Long,
    @ColumnInfo(name = "race_pere")
    @Json(name = "race_pere")
    var racePere: String,
    @ColumnInfo(name = "race_mere")
    @Json(name = "race_mere")
    var raceMere: String,
    @Embedded
    var category: Category,
    @Embedded
    var proprietaire: Proprietaire

)
/*{
    constructor() : this(0, 0, Espece(0,"",""), Elevage(0, ParametreGeographique("","","",
        Country("",""),""
    ),0), "", 0, "", "", Category(0,"",""),Proprietaire
    (0,"","","",""))
}
 */
data class Espece(
    @ColumnInfo(name = "id_espece")
    @Json(name = "id_espece")
    var id: Long,
    var fr : String,
    var en : String
)
data class Elevage(
    @ColumnInfo(name = "id_elevage")
    @Json(name = "id_elevage")
    var id: Long,
    @Embedded
    var parametregeographique: ParametreGeographique,
    @ColumnInfo(name = "code_elevage")
    @Json(name = "code_elevage")
    var codeElevage : Long
)
data class ParametreGeographique(
    var prefecture : String,
    var region : String,
    var commune : String,
    @Embedded
    var pays : Country,
    var village : String
)
data class Country(
    @ColumnInfo(name = "fr_pays")
    @Json(name = "fr_pays")
    var fr : String,
    @ColumnInfo(name = "en_pays")
    @Json(name = "en_pays")
    var en : String
)

data class Category (
    @ColumnInfo(name = "id_category")
    var id: Long,
    @ColumnInfo(name = "fr_category")
    var fr : String,
    @ColumnInfo(name = "en_category")
    var en : String
)
data class Proprietaire(
    @ColumnInfo(name = "id_proprietaire")
    var id: Long,
    var fullname : String,
    var telephone : String,
    @ColumnInfo(name = "telephone_second")
    var telephoneSecond : String,
    @ColumnInfo(name = "photo_profile")
    var photoProfil : String
)

The retrofit call

@GET("marche-betail/transport/{id}")
fun commandesDetailFromServer(@Path("id") id: Long?): Observable<Animal>

How can I handle the error. I tried to use constructor for Animaux class but I get error. I used also list<Animaux> but it didn't work.

Upvotes: 1

Views: 1947

Answers (1)

Alexey Romanov
Alexey Romanov

Reputation: 170713

Your JSON has a list of Animaux, not just a single one. So you need var animaux: List[Animaux], not just Animaux.

How do you implement it in the model class. Could you show how to do it becuase when I try I get this error error: Entities and POJOs must have a usable public constructor. You can have an empty constructor or a constructor whose parameters match the fields (by name and type). - java.util.List

Room doesn't support such fields directly. You could technically make it work (see answers to Android room persistent library - how to insert class that has a List object field), but your data is definitely complex enough that I wouldn't try to use a single class both for JSON and for Room; it'll end up with a very un-normalized database.

Upvotes: 2

Related Questions