Kotlin获得了瑞典的所有广播频道以回收观看

时间:2018-11-05 19:31:16

标签: json kotlin retrofit

我正在尝试获取瑞典的所有广播频道,并将它们添加到Kotlin的回收视图中。但是我的数据没有显示在回收视图中,我认为对api的调用是错误的。我需要调用此https://api.sr.se/api/v2/channels/来读取json数据并获取所有频道。如何通过翻新的方式修复以读取json?

  interface ApiChannels {

    @GET("https ://api.sr.se/api/v2/channels/")
    fun getChannels() : Observable<ChannelResponse>
}


 class Channel {
    var image : String? = null
    var imagetemplate : String?=null
    var color : String?=null
    var tagline :String?=null
    var siteurl : String?=null
    var id : Int = 0
    var url : URL? = null
    var statkey : String?=null
    var scheduleurl : String?=null
    var channeltype : String?=null
    var name : String?=null
}


class ChannelResponse {
    lateinit var data : List<Channel>
}

class HomeActivity : AppCompatActivity() {
    private lateinit var channelAdapter : ChannelsAdapter

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

        val recyclerView = findViewById<RecyclerView>(R.id.recycler)
        channelAdapter = ChannelsAdapter()
        recyclerView.addItemDecoration(DividerItemDecoration(this, DividerItemDecoration.VERTICAL))
        recyclerView.adapter = channelAdapter

        val retrofit : Retrofit = Retrofit.Builder()
                .baseUrl("https://api.sr.se/api/v2/channels/")
                .addConverterFactory(GsonConverterFactory.create())
                .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                .build()

        val apiChannels = retrofit.create(ApiChannels::class.java)

        apiChannels.getChannels()
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.computation())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe({ channelAdapter.setChannels(it.data) },
                        {
                            Toast.makeText(applicationContext, it.message, Toast.LENGTH_SHORT).show()
                        })
    }



    inner class ChannelsAdapter : RecyclerView.Adapter<ChannelsAdapter.ChannelViewHolder>() {

        private val channels: MutableList<Channel> = mutableListOf()

        override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ChannelViewHolder {
            return ChannelViewHolder(layoutInflater.inflate(R.layout.listchannels, parent, false))
        }

        override fun getItemCount(): Int {
            return channels.size
        }

        override fun onBindViewHolder(holder: ChannelViewHolder, position: Int) {
            holder.bindModel(channels[position])
        }

        fun setChannels(data: List<Channel>) {
            channels.addAll(data)
            notifyDataSetChanged()
        }

        inner class ChannelViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
            val idText : TextView = itemView.findViewById(R.id.channel)
            val songText : TextView = itemView.findViewById(R.id.song)
            val artistText : TextView = itemView.findViewById(R.id.artist)
            val channelImage : ImageView = itemView.findViewById(R.id.imageView5)

            fun bindModel(channel: Channel) {
                idText.text = channel.id.toString()
                songText.text = channel.name
                artistText.text = channel.ch
                Picasso.get().load(channel.image).into(channelImage)
            }
        }
    }
}

0 个答案:

没有答案