I'm making a project (for a programming class in uni) but when I try to run it in Android Studio, a warning appears very briefly in the emulator:
java.lang.IllegalStateException: Expected BEGIN_OBJECT but was STRING at line 5 column 21 path $[0].dateOfBirth
This is my JSON file:
{
"fighters": [
{
"id": 1,
"name": "Karl",
"dateOfBirth": "20-03-1975",
"level": 4,
"image": "karl.png"
},
{
"id": 2,
"name": "Geralt",
"dateOfBirth": "16-08-1964",
"level": 8,
"image": "Geralt.png"
},
{
"id": 3,
"name": "Darrak",
"dateOfBirth": "25-11-1940",
"level": 5,
"image": "Darrak.png"
},
{
"id": 4,
"name": "Jafar",
"dateOfBirth": "09-02-1920",
"level": 9,
"image": "Jafar.png"
},
{
"id": 5,
"name": "Cornelius",
"dateOfBirth": "28-06-1988",
"level": 2,
"image": "Cornelius.png"
},
{
"id": 6,
"name": "Laila",
"dateOfBirth": "18-10-1998",
"level": 5,
"image": "Laila.png"
},
{
"id": 7,
"name": "Marianne",
"dateOfBirth": "01-03-1975",
"level": 7,
"image": "Marianne.png"
},
{
"id": 8,
"name": "Petro",
"dateOfBirth": "10-07-1974",
"level": 10,
"image": "Petro.png"
},
{
"id": 9,
"name": "Ordelia",
"dateOfBirth": "18-05-1985",
"level": 5,
"image": "Ordelia.png"
},
{
"id": 10,
"name": "Lucina",
"dateOfBirth": "21-09-1992",
"level": 9,
"image": "Lucina.png"
},
{
"id": 11,
"name": "Hugo",
"dateOfBirth": "16-07-1938",
"level": 6,
"image": "Hugo.png"
},
{
"id": 12,
"name": "Sildar",
"dateOfBirth": "19-12-1980",
"level": 3,
"image": "Sildar.png"
},
{
"id": 13,
"name": "Zenok",
"dateOfBirth": "30-10-1999",
"level": 1,
"image": "Zenok.png"
},
{
"id": 14,
"name": "Violet",
"dateOfBirth": "02-04-2001",
"level": 8,
"image": "Violet.png"
},
{
"id": 15,
"name": "Tamara",
"dateOfBirth": "13-06-1963",
"level": 4,
"image": "Tamara.png"
}
],
"encounters": [
{
"id": 1,
"fighterId": 1,
"amount_of_monsters": 4,
"difficulty": "Medium"
},
{
"id": 2,
"fighterId": 5,
"amount_of_monsters": 1,
"difficulty": "Easy"
},
{
"id": 3,
"fighterId": 5,
"amount_of_monsters": 2,
"difficulty": "Medium"
},
{
"id": 4,
"fighterId": 7,
"amount_of_monsters": 1,
"difficulty": "Hard"
},
{
"id": 5,
"fighterId": 11,
"amount_of_monsters": 7,
"difficulty": "Medium"
},
{
"id": 6,
"fighterId": 7,
"amount_of_monsters": 2,
"difficulty": "Easy"
},
{
"id": 7,
"fighterId": 14,
"amount_of_monsters": 10,
"difficulty": "Extreme"
},
{
"id": 8,
"fighterId": 13,
"amount_of_monsters": 4,
"difficulty": "Medium"
},
{
"id": 9,
"fighterId": 7,
"amount_of_monsters": 5,
"difficulty": "Hard"
},
{
"id": 10,
"fighterId": 3,
"amount_of_monsters": 5,
"difficulty": "Easy"
}
]
}
Line 5 is "name": "Karl",
There's something wrong with my dateOfBirth attribute and I don't know why, because to me the syntax looks correct. I've tried reinstalling the app and rebuilding the project but that didn't work.
This is my first time posting a question on StackOverflow, so apologies if something isn't clear. If anyone is able to help, I would greatly appreciate it.
EDIT (additional info)
I'm using Android Studio 3.6, API 29.
Android Gradle Plugin Version: 3.5.3
Gradle Version: 5.4.1
I use GSON to parse the JSON
JDK 11
This is the function I used for the GSON class:
fun getFighters(): Observable<Array<Fighter>> {
val observable = Observable.create<Array<Fighter>> { emitter ->
try {
var connection = connect("${BASE_URL}/fighters")
val gson = GsonBuilder().create()
val fighters = gson.fromJson(
InputStreamReader(connection.inputStream),
Array<Fighter>::class.java
)
for (fighter in fighters) {
connection = connect("${BASE_URL}/${fighter.image}")
fighter.imageBitmap = BitmapFactory.decodeStream(connection.inputStream)
}
emitter.onNext(fighters)
} catch(e: Exception) {
emitter.onError(e)
}
}
return observable
}
The BASE_URL is just a testing url since the assignment we have to do doesn't require to actually deploy the app. The for loop is to show the images of the Fighters in a RecyclerView list, so therefore I used a bitmap. Furthermore, here's the basic Fighter data class:
data class Fighter(
val id: Number,
val name: String,
val dateOfBirth: LocalDate,
val level: Number,
val image: String,
var imageBitmap: Bitmap
)
I think the class that you are using for parsing the JSON should be modified as follows.
data class Fighter(
val id: Number,
val name: String,
val dateOfBirth: String,
val level: Number,
val image: String,
var imageBitmap: Bitmap
)
The dateOfBirth
is stored as a String
in your JSON and you need to fetch that in that way. If you need to convert the value to a LocalDate
object, you can always do that later after parsing the information. I hope that helps!