2

I am reading in the following JSON and using GSON to convert it to an object which I can then use to access the properties of said object to help display the images in my app.

However one of the field I want to use, imageURL, is returning null values. Looking at the json (link below) we can clearly see that it is not null.

https://api.letsbuildthatapp.com/youtube/home_feed

I have used the debugger to demonstrate the null value I am getting for each imageURL:

Debugger output

So the object is giving me null values for the imageURL but the link is not. What is going on?

Here is the function I wrote to fetch and parse the JSON object:

private fun fetchJson() {
        println("Attempting to fetch JSON")

        val url = "https://api.letsbuildthatapp.com/youtube/home_feed"
        val request = Request.Builder().url(url).build()
        val client = OkHttpClient()

        client.newCall(request).enqueue(object : Callback {
            override fun onResponse(call: Call, response: Response) {
                val body = response.body?.string()
                println(body)

                val gson = GsonBuilder().create()

                val homeFeed = gson.fromJson(body, HomeFeed::class.java)

                runOnUiThread {
                    recyclerView_main.adapter = MainAdapter(homeFeed)
                }
            }

            override fun onFailure(call: Call, e: IOException) {
                println("failed to execute request")
            }
        }
         )

    }

My HomeFeed class is like this:


class HomeFeed(val videos: List<Video>)

class Video(val id: Int, val name: String, val link: String, val imageURL: String, numberOfViews: Int,
            val channel: Channel)

class Channel(val name: String, val profileImageUrl: String)

I believe this should be detailed enough but if anymore info is needed please let me know.

Thank you.

2
  • Can you add your model of your object ... Commented Feb 11, 2020 at 21:49
  • I have just added that Commented Feb 11, 2020 at 22:01

1 Answer 1

2

Try with this object, you should use the same name for the vals, "imageUrl" instead of "imageURL":

data class HomeFeed(
    val user: User,
    val videos: List<Video>
)

data class User(
    val id: Int,
    val name: String,
    val username: String
)

data class Video(
    val channel: Channel,
    val id: Int,
    val imageUrl: String,
    val link: String,
    val name: String,
    val numberOfViews: Int
)

data class Channel(
    val name: String,
    val numberOfSubscribers: Int,
    val profileImageUrl: String
)
Sign up to request clarification or add additional context in comments.

1 Comment

I spent an hour trying to fix this.....and it is just a typo :( Thanks for the help!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.