[英]Accessing a kotlin class auto-generated getter method in a retrofit implementation in an Android application
我正在嘗試使用kotlin語言在android應用程序中實現翻新庫,但我打算使用kotlin的自動生成的getter功能來獲取MovieResponse類的值。
這是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
}
}
這是我的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)
}
})
}
}
我無法得到這個
@SerializedName("results")
private var results : List<Movie>? = null
從這里:
val movies : List<Movie> = response.body()
在普通的java中
val movies : List<Movie> = response.body().getResults()
但是在Kotlin中,我似乎無法實現這一點。
我對“電影數據庫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"
},
{...}
]
}
您的變量在MovieResponse
類中似乎是私有的,因此您不能從類外部訪問它。 檢查一下 。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.