multithreading – How to call nested API on Android asynchronously?-ThrowExceptions

Exception or error:

Let’s say I want to retrieve a list of news via my Android app, like this:

[{“title”: “Hello world”, “date”: “0102020”, “readCount: “44”}, {“title”: “foo bar”, “date”: “03022020”, “readCount: “200”}, {“title”: “This is awesome”, “readCount”: “315”}]

and display them using RecyclerView.

For unknown reasons, the API designer made the process less straightword. First you have to call /get-news-list, which return the list of news IDs, like this:

[312, 449, 518, 611, 623, 734, 755, 812]

Then with each ID as parameter, you call the /get-new-detail. For example /get-news-detail/312 returns:

{“title”: “Hello world”, “date”: “04020202”, “readCount”: “132”,
“shareCount”: “150”}

I’m thinking something like this:

class FetchNewsDetails extends AsyncTask<String, Void, String>{
// process /get-news-list first

 // now that we have the list of new IDs, call AsyncTask on each of them to retrieve the details
for (int x = 0; x < newsIDcount; x++){


I think my idea is cumbersome. Is there any way to simplify it?

How to solve:

I think you should give RxJava a try:


Then you can do the reading like this:

    fun getList(): Single<List<Long>> {
        TODO() //reading list of item keys

    fun getItem(itemKey: Long): Single<String> {
        TODO() //reading item

    fun doTheReading(compositeDisposable: CompositeDisposable) {
                .flattenAsObservable { it }
                .flatMapSingle { getItem(it) }
                .subscribe { t1, t2 -> }

You’ll also need to clear your compositeDisposable entity at some point, to make sure long running operation is not outliving your consumer.

Leave a Reply

Your email address will not be published. Required fields are marked *