Louis Sankey
Louis Sankey

Reputation: 492

Retrofit with Kotlin - having trouble modeling the data

So I'm new to retrofit and Kotlin, and in particular I'm not sure how to model my json response.

The full json response from the API looks like this:

{
    "programs": [
        {
            "id": "45MnVkbjoylSFJGS9iifLoP25HW-wZ2O",
            "url": "http://pac-12.com/videos/highlights-oregon-falls-vanderbilt-7-2",
            "title": "David Shaw eager to see Stanford football tested in non-conference play: 'We don't dodge competition'",
            "short_title": "David Shaw eager to see Cardinal tested in non-conference play: 'We don't dodge competition'",
            "description": "Unable to produce much offense against Commodores’ starting pitcher Carson Fulmer, Oregon fell to regional host Vanderbilt Saturday night 7-2. Fulmer pitched eight innings of two-run ball while striking out five and only allowing three hits.  A five-run fifth inning doomed the Ducks as they now must play Xavier Sunday, June 1 at 10 a.m. PT in an elimination game. Should the Ducks get past Xavier, they will then meet Vanderbilt once again later that day at 5:00 p.m. PT.",
            "duration": 30030,
            "locked": false,
            "pac_12_now": true,
            "published_by": "Stanford",
            "content_types": [
                {
                    "type": "Highlights"
                },
                {
                    "type": "Feature"
                }
            ],
            "sports": [
                {
                    "id": 6
                }
            ],
            "schools": [
                {
                    "id": 362,
                    "home_team": false
                },
                {
                    "id": 17,
                    "home_team": true
                }
            ],
            "events": [
                {
                    "id": "2014538e63f5b9369"
                }
            ],
            "metatags": [
                {
                    "name": "Baseball"
                }
            ],
            "images": {
                "large": "http://x.pac-12.com/sites/default/files/styles/ooyala_video_preview_image_medium/public/RHCYBEQYSCXLPKA.20140601024724.jpg?itok=llStxuKN",
                "medium": "http://x.pac-12.com/sites/default/files/styles/ooyala_video_preview_image_medium/public/RHCYBEQYSCXLPKA.20140601024724.jpg?itok=llStxuKN",
                "small": "http://x.pac-12.com/sites/default/files/styles/ooyala_video_preview_image_small/public/RHCYBEQYSCXLPKA.20140601024724.jpg?itok=GcNuXFdA",
                "tiny": "http://x.pac-12.com/sites/default/files/styles/ooyala_video_preview_image_extra_small/public/RHCYBEQYSCXLPKA.20140601024724.jpg?itok=YSHdwd8a"
            },
            "email_image": "https://x.pac-12.com/sites/default/files/styles/vod_email_watermark/public/img_11923693__1564769028.jpg?itok=4O20YDGH",
            "follow_on_vod_id": "gxdmxibjq7FxK5pvCHyfT9ELh6xc7YTq",
            "manifest_url": "http://pac12-vod.storage.googleapis.com/652991-master-playlist.m3u8",
            "campaign": null,
            "ad_params": {
              "schools": "stan",
              "sports": "football",
            },
            "playlists": [
                1113756
            ],
            "created": "2014-06-01T03:03:47Z",
            "updated": "2014-06-01T03:23:07Z"
        }
    ],
    "next_page": "/v3/vod?page=2&pagesize=1"
}

I made a data class named VOD with SOME of the properties of the response. These are the only properties I need, and I don't know if I need to include ALL the properties for it to work??

data class VOD(
    var title: String,
    var manifest_url: String,
    var thumbnail: String,
    var duration: Int,
    var schools: JSONArray,
    var sports: JSONArray,
)

I made an interface class

interface ApiInterface {

    @GET("vod")
    fun getVODs() : Call<ArrayList<VOD>>

    companion object {

        var BASE_URL = "https://api.pac-12.com/v3/"

        fun create() : ApiInterface {

            val retrofit = Retrofit.Builder()
                .addConverterFactory(GsonConverterFactory.create())
                .baseUrl(BASE_URL)
                .build()
            return retrofit.create(ApiInterface::class.java)

        }
    }
}

And I'm calling this in my onCreate


            val apiInterface = ApiInterface.create().getVODs()

            //apiInterface.enqueue( Callback<List<Movie>>())
            apiInterface.enqueue( object : Callback<ArrayList<VOD>>{
                override fun onResponse(call: Call<ArrayList<VOD>>?, response: Response<ArrayList<VOD>>?) {

                    if(response?.body() != null)
                        binding.recyclerView.setVODs(response.body()!!)
                }

                override fun onFailure(call: Call<ArrayList<VOD>>, t: Throwable) {

                }
            })

I noticed that for modeling I may need to use @Serialize in my data class. So my main questions are:

  1. do I need to use @Serialize in my data classes?
  2. do I need to model the ENTIRE json response?

Thanks.

Upvotes: 0

Views: 140

Answers (1)

Zaid Zakir
Zaid Zakir

Reputation: 563

Hi in regards to both ur questions

  1. Serializable is not part of android even though its used to pass data between activities, the downside is it creates objects using reflection and causes alot garbage collection, which might affect performance, its recommended to use Parcelable because it doesn't use reflection. Implementation might vary by little. not really a big deal to start using Parcelable if you want. https://developer.android.com/reference/android/os/Parcelable

  2. no you don't need to model the ENTIRE json response, you can model only whats needed for your work.Because some open source APIs send in info you dont need for your project, depends on your needs

Btw you do not need to manually create data classes, there is a plugin in android studio called JSON To Kotlin Class ​(JsonToKotlinClass)​ - https://plugins.jetbrains.com/plugin/9960-json-to-kotlin-class-jsontokotlinclass-

jst copy paste your response into in and it will create data classes in seconds

Upvotes: 1

Related Questions