使用 Kotlin 进行改造 - 在数据建模时遇到问题

Retrofit with Kotlin - having trouble modeling the data

所以我是改造和 Kotlin 的新手,尤其是我不确定如何为我的 json 响应建模。

来自 API 的完整 json 响应如下所示:

{
    "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"
}

我制作了一个名为 VOD 的数据 class,其中包含响应的一些属性。这些是我唯一需要的属性,我不知道是否需要包含所有属性才能正常工作??

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

我做了一个界面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)

        }
    }
}

我在 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) {

                }
            })

我注意到为了建模,我可能需要在我的数据中使用@Serialize class。所以我的主要问题是:

  1. 我需要在我的数据中使用 @Serialize classes 吗?
  2. 我需要对整个 json 响应进行建模吗?

谢谢。

你好,关于你的两个问题

  1. Serializable 不是 android 的一部分,即使它用于在活动之间传递数据,缺点是它使用反射创建对象并导致大量垃圾收集,这可能会影响性能,它的建议使用 Parcelable 因为它不使用反射。实施可能略有不同。如果你愿意,开始使用 Parcelable 并不是什么大不了的事。 https://developer.android.com/reference/android/os/Parcelable

  2. 不,您不需要对整个 json 响应进行建模,您可以仅对 work.Because 所需的内容进行建模,一些开源 API 会发送您不需要的信息对于您的项目,取决于您的需求

顺便说一句,你不需要手动创建数据类,在android studio 中有一个名为JSON To Kotlin Class 的插件(JsonToKotlinClass) - https://plugins.jetbrains.com/plugin/9960-json-to-kotlin-class-jsontokotlinclass-

jst 将您的回复复制粘贴到 in 中,它将在几秒钟内创建数据 类