Skip to content

Snappy1

  • Home
  • Android
  • What
  • How
  • Is
  • Can
  • Does
  • Do
  • Why
  • Are
  • Who
  • Toggle search form

[FIXED] android – Retrofit Enqueue parsing a Json object without an additional class

Posted on November 11, 2022 By

Solution 1 :

I came up with something that worked, but I don’t think it is ideal.
Rather than use the GsonConverter that is inbuild to retrofit, I do it myself.

inside onResponse I do the following

val peopleList = GsonBuilder()
                    .create()
                    .fromJson(response.body()!!.getAsJsonArray("people"), Array<Person>::class.java).toList()
Log.d("First person", peopleList!![0])

I am not at all sure this is better then just having extra data classes though

Problem :

I have working code that allows me parse the Json files I get from a retrofit API get call. However, the way I am currently doing it requires two classes (one of which is just a list containing the other), and if I was wondering if it is possible to do this with a single data class. More explanation is below.

What I have:

Interface:

interface ApiInterface {
    @GET(value = "all_people.php")
    fun getAllPeople(): Call<People>
}

Code:

retrofit: ApiInterface = Retrofit.Builder()
        .addConverterFactory(GsonConverterFactory.create())
        .baseUrl(BASE_URL)
        .build()
        .create(ApiInterface::class.java)
retrofit.getAllPeople().enqueue(object : Callback<People?> {
            override fun onResponse(call: Call<People?>, response: Response<People?>) {
               Log.d("First person", responce.body()!!.people[0])
            }
            override fun onFailure(call: Call<People?>, t: Throwable) {}
        })

Data classes:

data class Person (
    val firstName: String,
    val lastName: String
)
data class People (
    val people: List<Person>
)

THIS IS WORKING

The issue is that this requires an additional class (People). This is because I am getting back from the API a JSON object (Which contains the JSON array I want to access). This is the solution I have found online when I look at scenarios like this, however, this method requires me to create an additional class, containing just a list, for every different API call I have. Which is obviously not ideal.

Question: My question is how would I do this while eliminating the class People?

I want to do something like this:

Interface:

interface ApiInterface {
    @GET(value = "all_people.php")
    fun getAllPeople(): Call<List<Person>>
}

Code:

retrofit: ApiInterface = Retrofit.Builder()
        .addConverterFactory(GsonConverterFactory.create())
        .baseUrl(BASE_URL)
        .build()
        .create(ApiInterface::class.java)
retrofit.getAllPeople().enqueue(object : Callback<List<Person>?> {
            override fun onResponse(call: Call<List<Person>?>, response: Response<List<Person>?>) {
//The issue is here, because this is a Json object, and I am treating it like a list
//Is there a way of access the Json array inside this Json object without creating the person class?
               Log.d("First person", responce.body()!![0]) 
            }
            override fun onFailure(call: Call<List<Person>?>, t: Throwable) {}
        })

However, I can’t figure out how to “open” the Json obect to get this to work and therefore get this error code:

READ  [FIXED] android - Binding to cellular network with bindProcessToNetwork does not work
Powered by Inline Related Posts

Comments

Comment posted by Campino

responce.body() must be a object type List

Comment posted by Campino

if response.body is not already a List, then include the serialize in the .addConverterFactory, add it in onResponse is no a good idea.

Android Tags:android, gson, json, kotlin, retrofit2

Post navigation

Previous Post: [FIXED] Unable to connect Firebase in Android Emulator for Flutter Project
Next Post: [FIXED] android – Highlight only one Card in Compose through selection

Related Posts

[FIXED] java – Refresh anychart pie chart with new data Android
[FIXED] How to draw this shape in XML for Android? Android
[FIXED] how to install android studio 3.5.0 for offline use Android
[FIXED] android – Get information about current app in foreground Android
[FIXED] android – need help finishing activity in a Firebase QR code scanner in Kotlin Android
[FIXED] java – How to get all data at each month from Firebase for calculation in Android Android

Archives

  • March 2023
  • February 2023
  • January 2023
  • December 2022
  • November 2022
  • October 2022
  • September 2022

Categories

  • ¿Cómo
  • ¿Cuál
  • ¿Cuándo
  • ¿Cuántas
  • ¿Cuánto
  • ¿Qué
  • Android
  • Are
  • At
  • C'est
  • Can
  • Comment
  • Did
  • Do
  • Does
  • Est-ce
  • Est-il
  • For
  • Has
  • Hat
  • How
  • In
  • Is
  • Ist
  • Kann
  • Où
  • Pourquoi
  • Quand
  • Quel
  • Quelle
  • Quelles
  • Quels
  • Qui
  • Should
  • Sind
  • Sollte
  • Uncategorized
  • Wann
  • Warum
  • Was
  • Welche
  • Welchen
  • Welcher
  • Welches
  • Were
  • What
  • What's
  • When
  • Where
  • Which
  • Who
  • Who's
  • Why
  • Wie
  • Will
  • Wird
  • Wo
  • Woher
  • you can create a selvedge edge: You can make the edges of garter stitch more smooth by slipping the first stitch of every row.2022-02-04
  • you really only need to know two patterns: garter stitch

Recent Posts

  • Which is stronger 19 gauge or 23 gauge?
  • Does BMW still use GM transmissions?
  • Is primary or secondary market research better?
  • What does it mean when ADT says low battery?
  • Can a baby sleep on a regular mattress?

Recent Comments

No comments to show.

Copyright © 2023 Snappy1.

Powered by PressBook Grid Dark theme