Closed mbove77 closed 3 years ago
I doubt that is an issue related to retrofit, can you put a sample code for the whole flow?
I doubt that is an issue related to retrofit, can you put a sample code for the whole flow?
Now I added all the related code.
Can you express the problem in a failing test case or executable sample? It's impossible to know what your API endpoint is returning. By faking out its responses with MockWebServer such as the example test in the issue template or the suite of tests inside the library we can remove the remote API as being a factory and ensure that it's only the Retrofit code that we are testing. You can use tools like Postman or Charles Proxy to inspect the raw HTTP response in order to import it into a test.
@mbove77 I inspected your code the problem was in your deserializer.
class GsonDeserializador : JsonDeserializer<List<Foto>> {
// this is the problem **var fotos: MutableList<Foto> = ArrayList()**
@Throws(JsonParseException::class)
override fun deserialize(json: JsonElement, typeOfT: Type, context: JsonDeserializationContext): List<Foto> {
val photos = json.asJsonObject["photos"].asJsonArray
// this is the solution
var fotos: MutableList<Foto> = ArrayList()
for (i in 0 until photos.size()) {
val id = photos[i].asJsonObject["id"].asInt
val width = photos[i].asJsonObject["width"].asInt
val height = photos[i].asJsonObject["height"].asInt
val url = photos[i].asJsonObject["url"].asString
/**
more parsing
**/
val currentFoto = Foto(id, width, height, url, photographer, original, large, large2x, medium, small, portrait, landscape, tiny)
if (!fotos.contains(currentFoto)) fotos.add(currentFoto)
}
return fotos
}
}
I have this services call, the itemNum is: 15 by default.
@GET("curated") suspend fun getCurated(@Query("per_page") itemNum: Int, @Query("page") numPage: Int): Response<List<Photo>>
This returns a response object with a list of 15 items, the problem i am facing is in the first call to services returns the first 15 items from page 1, but in the second call it returns a list of 30 items the first 15 + 15 new.
Hope the list only contains the 15 items from each call. Somehow Retrofit stack the results.
This behavior starts when coroutines are implemented, before it works as I expected
I need only the 15 elementos of the current call, someone know how achieve that?
Here i copy the debug panel where you see the call parameters and the result elements.
here put all the flow
Gradle
Services:
Repository:
ViewModel Call:
Here put the response of the api in the page 1 and 2. api_response.zip
If you want you can see the repository of the project, you need to look for the "MVVM" branch and you also need to generate a Pexels Api key here: https://www.pexels.com/api/
Repository: https://github.com/mbove77/Pexels-Fotos-Wallpapers