I'm accessing a WordPress API to fetch the posts using Retrofit2, then when I try to assign the values to the views in my adapter:
inner class MainActivityViewHolder(view: View) : RecyclerView.ViewHolder(view) {
fun bind(post: Post) {
with(post) {
itemView.tv_post_title.text = title
itemView.tv_post_date.text = date
itemView.tv_post_content.text = content
}
}
}
The Retrofit's GET
returns an Observable
, and thus, the following error in onErrorResumeNext
is shown when it tries to assign:
java.lang.IllegalStateException: Expected a string but was BEGIN_OBJECT at line 1 column 373 path $[0].title
This is because title
key has a rendered
key within it:
{
id:497,
date:"2018-04-08T03:34:12",
[...]
title:{
rendered:"Lorem ipsum dolor sit amet"
}
}
The same applies to content
and excerpt
. How do I access these rendered
s keys? I tried something like
val title: JsonElement = JsonParser().parse(post.title)
but the same error persists.