Search code examples
androidmultithreadingrssandroid-asynctaskfeed

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> {
@Override
protected 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?


Solution

  • My thought process would be something like this:

    1. Create a list of all the feeds.
    2. From 1 to max number of threads, spawn a new thread to retrieve the data from the feed
    3. As a thread completes, checks list for remaining feeds to check; if more remain, remove feed from list and retrieve the data from the feed
    4. Each thread, once completed, adds the data for its retrieved feed to whatever data source you're using for the list adapter, sorts the data source, and notifies the list that the data set has changed.