Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- My podcast model
- data class Podcast(
- val feed: Feed
- )
- {
- data class Feed(
- val author: Author,
- val entry: List<Entry>,
- val updated: Updated,
- val rights: Rights2,
- val title: Title2,
- val icon: Icon,
- val link: List<Link2>,
- val id: Id2,
- )
- data class Author(
- val name: Name,
- val uri: Uri,
- )
- data class Name(
- val label: String,
- )
- data class Uri(
- val label: String,
- )
- data class Entry(
- @JsonProperty("im:name")
- val imName: ImName,
- @JsonProperty("im:image")
- val imImage: List<ImImage>,
- val summary: Summary,
- @JsonProperty("im:price")
- val imPrice: ImPrice,
- @JsonProperty("im:contentType")
- val imContentType: ImContentType,
- val rights: Rights?,
- val title: Title,
- val link: Link,
- val id: Id,
- @JsonProperty("im:artist")
- val imArtist: ImArtist,
- val category: Category,
- @JsonProperty("im:releaseDate")
- val imReleaseDate: ImReleaseDate,
- )
- data class ImName(
- val label: String,
- )
- data class ImImage(
- val label: String,
- val attributes: Attributes,
- )
- data class Attributes(
- val height: String,
- )
- data class Summary(
- val label: String,
- )
- data class ImPrice(
- val label: String,
- val attributes: Attributes2,
- )
- data class Attributes2(
- val amount: String,
- val currency: String,
- )
- data class ImContentType(
- val attributes: Attributes3,
- )
- data class Attributes3(
- val term: String,
- val label: String,
- )
- data class Rights(
- val label: String,
- )
- data class Title(
- val label: String,
- )
- data class Link(
- val attributes: Attributes4,
- )
- data class Attributes4(
- val rel: String,
- val type: String,
- val href: String,
- )
- data class Id(
- val label: String,
- val attributes: Attributes5,
- )
- data class Attributes5(
- @JsonProperty("im:id")
- val imId: String,
- )
- data class ImArtist(
- val label: String,
- val attributes: Attributes6?,
- )
- data class Attributes6(
- val href: String,
- )
- data class Category(
- val attributes: Attributes7,
- )
- data class Attributes7(
- @JsonProperty("im:id")
- val imId: String,
- val term: String,
- val scheme: String,
- val label: String,
- )
- data class ImReleaseDate(
- val label: String,
- val attributes: Attributes8,
- )
- data class Attributes8(
- val label: String,
- )
- data class Updated(
- val label: String,
- )
- data class Rights2(
- val label: String,
- )
- data class Title2(
- val label: String,
- )
- data class Icon(
- val label: String,
- )
- data class Link2(
- val attributes: Attributes9,
- )
- data class Attributes9(
- val rel: String,
- val type: String?,
- val href: String,
- )
- data class Id2(
- val label: String
- )
- My api client
- private lateinit var retrofit: Retrofit
- private val cacheSize = 10 * 1024 * 1024 // 10 MB
- private val cache = Cache(context.cacheDir, cacheSize.toLong())
- private val requestIterceptor = Interceptor { chain ->
- val url = chain.request().url.newBuilder().build()
- val request = chain.request().newBuilder()
- .url(url)
- .cacheControl(CacheControl.Builder().maxAge(24, TimeUnit.HOURS).build()) // Set max age to 24 hours
- .build()
- val response = chain.proceed(request)
- response.newBuilder()
- .removeHeader("Pragma")
- .removeHeader("Cache-Control")
- .header("Cache-Control", "public, max-age=${24 * 60 * 60}") // Set max age to 24 hours
- .build()
- }
- private val httpClient = OkHttpClient.Builder()
- .addInterceptor(requestIterceptor)
- .cache(cache)
- .build()
- fun getClient(): Retrofit{
- retrofit = Retrofit.Builder()
- .baseUrl("https://itunes.apple.com/")
- .client(httpClient)
- .addConverterFactory(GsonConverterFactory.create())
- .build()
- return retrofit
- }
- my api service
- interface ApiServices {
- @get:GET("rss/toppodcasts/limit=100/genre=1310/json")
- val getTopPodcasts: Call<Podcast>
- @GET("lookup")
- fun getPodcastDetails(@Query("id") podcastId: String?): Call<PodcastDetail>
- @GET("lookup")
- fun getPodcastEpisodes(
- @Query("id") podcastId: String?,
- ): Call<PodcastEpisodeDetail>
- }
- and the Main
- class MainActivity : AppCompatActivity() {
- private lateinit var binding: ActivityMainBinding
- private val podcastAdapter by lazy { PodcastAdapter() }
- private val api : ApiServices by lazy {
- ApiClient(this).getClient().create(ApiServices::class.java)
- }
- override fun onCreate(savedInstanceState: Bundle?) {
- super.onCreate(savedInstanceState)
- binding = ActivityMainBinding.inflate(layoutInflater)
- setContentView(binding.root)
- binding.apply {
- mainProgressBar.visibility = View.VISIBLE
- val callPodcastApi = api.getTopPodcasts
- callPodcastApi.enqueue(object : retrofit2.Callback<Podcast>{
- override fun onResponse(call: Call<Podcast>, response: Response<Podcast>) {
- mainProgressBar.visibility = View.GONE
- when(response.code()){
- // successful response
- in 200..299->{
- response.body().let { b ->
- b?.feed.let {data ->
- if (data != null){
- podcastAdapter.differ.submitList(MutableList(Podcast))
- mainRecycler.apply {
- layoutManager = LinearLayoutManager(this@MainActivity)
- adapter = podcastAdapter
- }
- }
- }
- }
- }
- // redirection response
- in 300..399->{}
- // client error response
- in 400..499 ->{}
- // server error response
- in 500..599->{}
- }
- }
- override fun onFailure(call: Call<Podcast>, t: Throwable) {
- mainProgressBar.visibility = View.GONE
- Log.e("onFailure", "Err : ${t.message}")
- }
- })
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement