I'm trying to parse the items (the books) of the Google Books API using Gson, but for some reason, the JsonArray size is stuck to 10 if I try to access an index greater than that it will crash, but there should be 10000+ objects in the array. I tried to parse it with the JSON library from the android studio but I had the exact same error.
public String parse(String jsonLine) {
JsonElement jelement = new JsonParser().parse(jsonLine);
System.out.println("jelement " + jelement.toString());
System.out.println("Number of objects " + jelement.getAsJsonObject().get("totalItems"));
JsonObject jobject = jelement.getAsJsonObject();
System.out.println("jobject " + jobject.toString());
JsonArray jarray = jobject.getAsJsonArray("items");
jobject = jarray.get(500).getAsJsonObject();
And the error:
Number of objects 10275
FATAL EXCEPTION: main Process: java.lang.IndexOutOfBoundsException: Invalid index 500, size is 10
I think this API is paginated API which means, for example, let's say you are making a call for Google Books API.
Now there may be thousands/millions of books. But for the efficient implementation of the API, they might send only first 10 books,
Example: API call: http://www.somewebsite.com/api/books The result will be a result of 10 books. But the JSON has total Books count. So using this you can access the paginated API
{
totalBooks: 100,
books: [
{
//Book1
},
{
//Book2
}
]
}
So now there are 100 books but still, you receive only 10 books. So to access 55th book, you might have to make a request like API: http://www.somewebsite.com/api/books/5
Response to this API call will have books from 51 to 60.
{
totalBooks: 100,
books: [
{
//Book51
},
{
//Book52
}
]
}