Home > Net >  saving to shared prefs - java.lang.IllegalStateException: Expected BEGIN_OBJECT but was BEGIN_ARRAY
saving to shared prefs - java.lang.IllegalStateException: Expected BEGIN_OBJECT but was BEGIN_ARRAY

Time:07-28

I'm going a little crazy here... I have a series of fragments where I am storing user input into shared preferences in order to create the user/user dog at the final onboarding fragment. I believed I fixed my model to reflect the JSON response, but I'm getting an error trying to set preferences in the photos fragment and I don't understand why. I'm thinking it's to do with the photo model, or the fact that it's an arrayList in my dog object, but I'm not sure.

here's my JSON response object:

{"dogID":"9","name":"dog","pedigree":"","breederName":"","color":"1","breed":"2","birthday":"1222","userID":"294","gender":"2","previousLitters":"0","isPregnant":"0","nextHeat":"0","previousSires":"1","litters":"0","isAKC":"1","user":{"userID":"294","email":"[email protected]","name":"John","termsAccepted":"1","userType":"0","messageNotifications":"0","matchesNotifications":"0","timeCreated":"2022-06-13 04:24:31","timeUpdated":"2022-06-22 14:01:15","zipCode":"10036","lat":"40.75921249","lng":"-73.98463440","distance":"64","profilePic":"http://res.cloudinary.com/matchbreed/image/upload/v1655094371/k1yapllywjburm5m0rhs.jpg","breeds":"Belgian Tervuren","isBreeder":"1","autoShare":"1","isSearching":"1","location":"49 St","dogs":[]},"dogHealth":{"firstTemperament":"0","secondTemperament":"0","thirdTemperament":"0","isVetEvaluated":"1","vaccinations":"0","hips":"0","elbows":"0","heart":"0","other":""},"dogTitles":["0"],"dogPhotos":[{"photoUrl":"ksldsldksdk","isPrimary":"1"},{"photoUrl":"url2","isPrimary":"0"}]}

Here's my dog model:

@Parcelize
class DogModel(

    var birthday: String = "",
    var breed: String = "",
    var breederName: String = "",
    var color: String = "",
    var dogHealth: DogHealth = DogHealth(),
    var dogID: String = "",
    var dogPhotos: ArrayList<DogPhoto> = ArrayList(),
    var dogTitles: ArrayList<String> = ArrayList(),
    var gender: String = "",
    var isAKC: String = "",
    var isPregnant: String = "",
    var litters: String = "",
    var name: String = "",
    var nextHeat: String = "",
    var pedigree: String = "",
    var previousLitters: String = "",
    var previousSires: String = "",
    var user: User = User(),
    var userID: String = "",
):Parcelable {
    companion object {

    }

dog photos object:

@Parcelize
data class DogPhoto(
    val isPrimary: String = "",
    var photoUrl: String = ""
): Parcelable{}

I'm getting an error in my fragment here when setting my variables - private var dog: DogModel = getDogPref()

here are my preferences:

fun getDogPref(): DogModel {
    val u = getStringPref(PREF_DOG)
    if(u == ""){
        return DogModel()
    }
    val dog = JsonParser.parseString(u)
    return Gson().fromJson(dog, DogModel::class.java)
}

fun setDogPref(dog: DogModel) {
    val dogString = Gson().toJson(dog)//mapper.writeValueAsString(dog)
    setStringPref(PREF_DOG, dogString)
}

I can't see where I'm off... any help would be greatly appreciated!!

oh and here's my DogHealth model if needed:

@JsonIgnoreProperties(ignoreUnknown = true)
@Parcelize
data class DogHealth(
    @PrimaryKey(autoGenerate = true)
    var dogId: Int = 0,

    var elbows: String = "",
    var firstTemperament: String = "",
    var heart: String = "",
    var hips: String = "",
    var isVetEvaluated: String = "",
    var other: String = "",
    var secondTemperament: String = "",
    var thirdTemperament: String = "",
    var vaccinations: String = ""
): Parcelable {}

CodePudding user response:

Sharedpreferences is for primitive data in key-value pairs, so you can't use an arrayList or object as a value.

CodePudding user response:

I do it in the following way and it works very well for me

To save

val dogJson = Gson().toJson(dog)
preferences.edit.putString("dog_${dog.id}", dogJson).apply()

To read

val dogJson = preferences.getString("dog_$id")
dogJson ?: return
val dog = Gson().fromJson (dogJson, DogModel::class.java)

Maybe the problem is that you are saving it with Gson but reading with JsonParser. Try reading it the same way with Gson.

  • Related