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.
Here is the MovieResponse class:
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:
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
val movies : List<Movie> = response.body().getResults()
but in kotlin I can't seem to be able to implement that.
My response from the "The Movie database 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"
},
{...}
]
}