In my project I have run into some issues regarding returning nested JSON data using Kotlin, Retrofit and RxJava inside an MVVM pattern.
I'm relatively new to RxJava, MVVM and Retrofit and am quite confused still, but I would like to end up with having a list of a a select category that is currently nested inside a JSON.
The JSON response looks something like this...
{
"status": "ok",
"totalResults": 38,
"articles": [
{
"source": {
"id": "business-insider",
"name": "Business Insider"
},
"author": "Hayley Peterson",
"title": "Amazon executive was killed after colliding with a van delivering the company's packages, report reveals - Business Insider",
"description": "\"I heard a scream, immediately followed by a crash,\" the van's driver testified, according to the report.",
"url": "https://www.businessinsider.com/amazons-joy-covey-killed-company-delivery-van-report-2019-12",
"urlToImage": "https://image.businessinsider.com/5e01376e855cc215577a09f1?width=1200&format=jpeg",
"publishedAt": "2019-12-23T22:32:01Z",
"content": "The former Amazon executive Joy Covey was killed after colliding with a van delivering Amazon packages, according to an explosive investigation into the company's logistics network by BuzzFeed News and ProPublica. \r\nCovey was Amazon's first chief financial of… [+1462 chars]"
},
...
My data class looks like this...
data class Base (
@SerializedName("status") val status : String,
@SerializedName("totalResults") val totalResults : Int,
@SerializedName("articles") val articles : List<Story>
)
data class Story (
@SerializedName("source") val source : Source,
@SerializedName("author") val author : String,
@SerializedName("title") val title : String,
@SerializedName("description") val description : String,
@SerializedName("url") val url : String,
@SerializedName("urlToImage") val urlToImage : String,
@SerializedName("publishedAt") val publishedAt : String,
@SerializedName("content") val content : String
)
data class Source (
@SerializedName("id") val id : String,
@SerializedName("name") val name : String
)
I first use my API with @GET annotation to get top headlines with this code...
interface StoriesApi {
@GET("v2/top-headlines?country=us&apiKey=###MYKEY###")
fun getStories(): Single<List<Story>>
}
Which in turn is used by my StoriesService to get a Single>
class StoriesService {
@Inject
lateinit var api: StoriesApi
init {
DaggerApiComponent.create().inject(this)
}
fun getStories(): Single<List<Story>> {
return api.getStories()
}
}
And to end I call it in my ViewModel by using the code below...
private fun fetchStories() {
loading.value = true
disposable.add(
storiesService.getStories()
.subscribeOn(Schedulers.newThread())
.observeOn(AndroidSchedulers.mainThread())
.subscribeWith(object: DisposableSingleObserver<List<Story>>() {
override fun onSuccess(value: List<Story>?) {
stories.value = value
storyLoadError.value = false
loading.value = false
}
override fun onError(e: Throwable?) {
storyLoadError.value = true
loading.value = false
}
})
)
}
Is there any way that I can only enter the articles part of the JSON so that I don't have to fiddle too much with the whole JSON response? I would ultimately like to just end up with a Single> of the articles and not the "status" and "totalResults".