-
-
Save ruan65/49e7208c542e9e048a694fa4258b88f0 to your computer and use it in GitHub Desktop.
#Kotlin #Android Retrofit2 singleton instance sample
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package rubenpla.develop.privtmdbendlesslist.data.api | |
import retrofit2.Retrofit | |
import retrofit2.converter.gson.GsonConverterFactory | |
import retrofit2.http.GET | |
import retrofit2.http.Query | |
import rubenpla.develop.privtmdbendlesslist.data.model.MoviesResultsItem | |
interface TmdbApi { | |
@GET("movie/popular") | |
fun getPopularMovies(@Query("api_key") api_key: String, | |
@Query("page") page: Int): List<MoviesResultsItem> | |
companion object Factory { | |
@Volatile | |
private var retrofit : Retrofit? = null | |
private const val BASE_URL: String = "https://api.themoviedb.org/3/" | |
@Synchronized | |
fun getInstance(): TmdbApi? { | |
retrofit ?: synchronized(this) { | |
retrofit ?: buildRetrofit() | |
} | |
return retrofit?.create(TmdbApi::class.java) | |
} | |
private fun buildRetrofit() = retrofit2.Retrofit.Builder() | |
.baseUrl(BASE_URL) | |
.addConverterFactory(GsonConverterFactory.create()) | |
.build() | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment