Quantcast
Viewing all articles
Browse latest Browse all 3

Android: Reading multiple rss feeds as fast as possible using asynctask / threads

I have an app that reads about 3-5 rss feeds and present the headlines on the UI. I've put the reading code inside an asynctask to keep the UI responsive. But my code reads the feeds one at a time and I would like to read the 3 rss feeds at the same to see if I can speed up the parsing process and present the headlines faster on the UI.

I've tried to use threads - but then I ran into the problem that I didn't knew which thread would finish first, second and last and.. well, I just couldn't figure out how to check when the slowest thread had finished so I could sort the rss news objects by date and time. So eventually I tried to use asynctask reading the feeds one at a time like this:

class ReadFeedsTask extends AsyncTask<Void, String, Void> {@Overrideprotected Void doInBackground(Void... unused) {    try {        URL url = new URL("http://www.fyens.dk/rss/sport");        HttpURLConnection conn = (HttpURLConnection) url.openConnection();        if (conn.getResponseCode() == HttpURLConnection.HTTP_OK) {            InputStream is = conn.getInputStream();            DocumentBuilderFactory dbf = DocumentBuilderFactory                    .newInstance();            DocumentBuilder db = dbf.newDocumentBuilder();            Document document = db.parse(is);            Element element = document.getDocumentElement();            NodeList nodeList = element.getElementsByTagName("item");            if (nodeList.getLength() > 0) {                for (int i = 0; i < nodeList.getLength(); i++) {                    Element entry = (Element) nodeList.item(i);                    Element _titleE = (Element) entry.getElementsByTagName("title").item(0);                    Element _descriptionE = (Element) entry                            .getElementsByTagName("description").item(0);                    Element _pubDateE = (Element) entry                            .getElementsByTagName("pubDate").item(0);                    Element _linkE = (Element) entry.getElementsByTagName("link").item(0);                    String _title = _titleE.getFirstChild().getNodeValue();                    String _description = _descriptionE.getFirstChild().getNodeValue();                    Date _pubDate = new Date(_pubDateE.getFirstChild().getNodeValue());                    String _link = _linkE.getFirstChild().getNodeValue();

etc..

and then I repeat this process for the other rss feeds.

How can I use this code to read multiple feeds at the same time - if not by using asynctasc then by using threads? My problem is that I'm not able to understand how to wait untill all threads have finished so I can sort the results and present the newest first?


Viewing all articles
Browse latest Browse all 3

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>