简体   繁体   English

在Android应用程序的改造实现中访问kotlin类自动生成的getter方法

[英]Accessing a kotlin class auto-generated getter method in a retrofit implementation in an Android application

I am trying to implement the retrofit library in an android application using the kotlin language but I stock at the point of trying to get the a value of my MovieResponse class using kotlin's auto-generated getter capability. 我正在尝试使用kotlin语言在android应用程序中实现翻新库,但我打算使用kotlin的自动生成的getter功能来获取MovieResponse类的值。

Here is the MovieResponse class: 这是MovieResponse类:

class MovieResponse {

    @SerializedName("page")
    private var page : Int? = null

    @SerializedName("results")
    private var results : List<Movie>? = null

    @SerializedName("total_results")
    private var totalResults : Int? = null

    @SerializedName("total_pages")
    private var totalPages : Int? = null

    constructor(page: Int?, results: List<Movie>?, totalResults: Int?, totalPages: Int?) {
        this.page = page
        this.results = results
        this.totalResults = totalResults
        this.totalPages = totalPages
    }


}

And this is my android MainActivity class: 这是我的android MainActivity类:

class MainActivity : AppCompatActivity() {

    private val TAG : String = MainActivity::class.java.simpleName
    val BASE_URL : String = "http://api.themoviedb.org/3/"
    private var retrofit : Retrofit? = null
    private var recyclerView : RecyclerView? = null
    private var API_KEY : String = "166873e095bdb281691220d5ad12610c"

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        // setup the layout manager
        recycler_view.setHasFixedSize(true)
        recycler_view.layoutManager = LinearLayoutManager(this)

        // get the data
        connectAndGetData()

    }

    /**
     * This method creates an instance of Retrofit
     * and set the base url
     */
    private fun connectAndGetData() {
        if (retrofit == null) {
            retrofit = Retrofit.Builder()
                .baseUrl(BASE_URL)
                .addConverterFactory(GsonConverterFactory.create())
                .build()
        }

        val movieApiService = retrofit!!.create(MovieApiService::class.java)

        val call : Call<MovieResponse> = movieApiService!!.getTopRatedMovies(API_KEY)
        call.enqueue(object : Callback<MovieResponse> {
            override fun onFailure(call: Call<MovieResponse>, t: Throwable) {
            }

            override fun onResponse(call: Call<MovieResponse>, response: Response<MovieResponse>) {
                val movies : List<Movie> = response.body() #<-- stuck here
                recyclerView!!.adapter = MoviesAdapter(movies, R.layout.list_item_movie, applicationContext)
                Log.d(TAG, "Number of movies received: " + movies.size)
            }
        })


    }
}

I am unable to get this 我无法得到这个

@SerializedName("results")
        private var results : List<Movie>? = null

from here: 从这里:

val movies : List<Movie> = response.body()

In normal java it would be 在普通的java中

val movies : List<Movie> = response.body().getResults()

but in kotlin I can't seem to be able to implement that. 但是在Kotlin中,我似乎无法实现这一点。

My response from the "The Movie database API": 我对“电影数据库API”的回答:

{
    "page": 1,
    "total_results": 7444,
    "total_pages": 373,
    "results": [
        {
            "vote_count": 2080,
            "id": 19404,
            "video": false,
            "vote_average": 9,
            "title": "Dilwale Dulhania Le Jayenge",
            "popularity": 16.5,
            "poster_path": "\/uC6TTUhPpQCmgldGyYveKRAu8JN.jpg",
            "original_language": "hi",
            "original_title": "दिलवाले दुल्हनिया ले जायेंगे",
            "genre_ids": [
                35,
                18,
                10749
            ],
            "backdrop_path": "\/mMaxGuQKuH4WUHBwUNhJOetDYE9.jpg",
            "adult": false,
            "overview": "Raj is a rich, carefree, happy-go-lucky second generation NRI. Simran is the daughter of Chaudhary Baldev Singh, who in spite of being an NRI is very strict about adherence to Indian values. Simran has left for India to be married to her childhood fiancé. Raj leaves for India with a mission at his hands, to claim his lady love under the noses of her whole family. Thus begins a saga.",
            "release_date": "1995-10-20"
        },
        {...}
     ]
}

Your variable seems private in MovieResponse class so you can not access to it from outside of the class. 您的变量在MovieResponse类中似乎是私有的,因此您不能从类外部访问它。 Check this . 检查一下

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM