Parsing XML response from RSS feed in Android have always been a bit complicated. All this issues are combiend in very good library RSS-Parser. As the repo says and in this example, “RSS Parser is an Android library to parse any RSS feed. With RSS Parser, you can fetch plenty of useful information from any RSS channel, be it a blog, magazine, or even a podcast feed“.
Step one:
Implement as dependancy in app/build.gradle
1 2 3 |
dependencies { implementation 'com.prof18.rssparser:rssparser:<latest-version>' } |
Step two:
Initialise Parser SDK:
1 2 3 4 5 |
val parser = Parser.Builder() .context(this) .charset(Charset.forName("ISO-8859-7")) .cacheExpirationMillis(24L * 60L * 60L * 100L) // one day .build() |
This kind of implementation adds caching feature which can be very helpful if network is not available.
1 2 3 4 5 |
private fun getRssFeed() { CoroutineScope(IO).launch { initParser() } } |
It needs to be wrap and launched with Coroutine function
1 2 3 4 5 6 7 8 9 10 11 12 |
private suspend fun initParser() { try { val channel = getParser().getChannel("https://rss.art19.com/apology-line") for (news in channel.articles) { newsList.add(News(news.image, news.author, news.content, news.description)) } //Here are your results } catch (e: Exception) { e.printStackTrace() } } |
And to clear the cache just flush it:
1 |
parser.flushCache(url) |
And if no caching is needed just build the Parser:
1 |
val parser = Parser.Builder().build() |
The library can be found on this link