我正在尝试从网址https://api.sr.se/api/v2/channels/?format=json解析json数据,并显示对Recyclerview的json响应。但是在Recyclerview中没有任何显示,我认为我对基本URL的调用是错误的。我要显示的是所有频道的名称和ID,然后前进到现在在每个频道中播放的那首歌。
有人可以帮我吗?
代码:
data class Complete(
@SerializedName("channels")
val channels: List<Channel>,
@SerializedName("copyright")
val copyright: String,
@SerializedName("pagination")
val pagination: Pagination
)
data class Channel(
@SerializedName("channeltype")
val channeltype: String,
@SerializedName("color")
val color: String,
@SerializedName("id")
val id: Int,
@SerializedName("image")
val image: String,
@SerializedName("imagetemplate")
val imagetemplate: String,
@SerializedName("liveaudio")
val liveaudio: Liveaudio,
@SerializedName("name")
val name: String,
@SerializedName("scheduleurl")
val scheduleurl: String,
@SerializedName("siteurl")
val siteurl: String,
@SerializedName("tagline")
val tagline: String,
@SerializedName("xmltvid")
val xmltvid: String
)
class RadioItemAdapter(val postList: List<Channel>, val context: Context) :
RecyclerView.Adapter<RadioItemAdapter.ViewHolder> (){
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
return ViewHolder(LayoutInflater.from(context).inflate(R.layout.radio_item_layout,
parent, false))
}
override fun getItemCount(): Int {
return 10
}
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
holder.itemView.txtPostTitle.text = postList.get(position).id.toString()
holder.itemView.txtPostBody.text = postList.get(position).toString()
}
class ViewHolder(view: View) : RecyclerView.ViewHolder(view)
}
interface Network {
@GET("api/v2/channels/?format=json")
fun getAllRadio(): Observable<List<Channel>>
}
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
rv__list_posts.layoutManager = LinearLayoutManager(this)
val retrofit = Retrofit.Builder().addConverterFactory(GsonConverterFactory.create(GsonBuilder().create()))
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
.baseUrl("https://api.sr.se/").build()
val postsApi = retrofit.create(Network::class.java)
var response = postsApi.getAllRadio()
response.observeOn(AndroidSchedulers.mainThread()).subscribeOn(IoScheduler()).subscribe {
rv__list_posts.adapter = RadioItemAdapter(it,this)
}
}
}