Android feed.addItem() 不添加最后一项

Android feed.addItem() doesn't add last item

我不明白为什么我的代码没有将所有 10 个项目添加到我的 Feed 中,而只添加了 9 个!

循环探索了所有 10 项,但没有添加最后一项,我用 Log.e 进行了检查。

代码如下:

public class Parser {

    private Feed feed = new Feed();
    private ArrayList<String> categorie = new ArrayList<String>();

    public Feed parseXml(String xml) {
        int y = 1;
        URL url = null;
        try {
            url = new URL(xml);
        } catch (MalformedURLException e1) {
            e1.printStackTrace();
        }

        /* Tentativo di connessione */
        try {
            DocumentBuilderFactory dbf;
            dbf = DocumentBuilderFactory.newInstance();
            DocumentBuilder db = dbf.newDocumentBuilder();

            /* Parse della pagina */
            Document doc = db.parse(new InputSource(url.openStream()));
            doc.getDocumentElement().normalize();

            /* Prendi tutti i nodi "item" della pagina */
            NodeList nl = doc.getElementsByTagName("item");
            int length = nl.getLength();

            for (int i = 0; i < length; i++) {
                Node currentNode = nl.item(i);
                Item item = new Item();

                NodeList nchild = currentNode.getChildNodes();
                int clength = nchild.getLength();

                /* Prendi gli elementi necessari per ogni item */
                for (int j = 1; j < clength; j = j + 2) {

                    Node thisNode = nchild.item(j);
                    String theString = null;
                    String nodeName = thisNode.getNodeName();

                    theString = nchild.item(j).getFirstChild().getNodeValue();

                    /* Setta il titolo */
                    if (theString != null) {
                        if ("title".equals(nodeName)) {
                            item.setTitolo(theString);
                        }

                        /* Setta l'immagine */
                        else if ("description".equals(nodeName)) {                          
                            // Prendi il link all'immagine 
                            String html = theString;
                            org.jsoup.nodes.Document docHtml = Jsoup.parse(html);
                            Elements imgEle = docHtml.select("img");
                            item.setImmagine(imgEle.attr("src"));
                        }

                        /* Setta il testo */
                        else if ("content:encoded".equals(nodeName)) {                      
                            theString = pulisciTesto(theString);
                            int start = theString.indexOf("L'articolo " + item.getTitolo());
                            String theString2 = theString.substring(start);
                            theString = theString.replaceAll(theString2, "");
                            item.setTesto(theString);
                        }

                        /* Setta la data */
                        else if ("pubDate".equals(nodeName)) {

                            /* Pulisci la data */
                            String formatedDate = theString.replace(" +0000","");
                            item.setData(formatedDate);
                        }

                        /* Setta l'autore */
                        else if ("dc:creator".equals(nodeName)) {
                            item.setAutore(theString);
                        }

                        /* Setta la categoria */
                        else if ("category".equals(nodeName)) {
                            if (checkCategoria(theString) != null)
                                item.setCategoria(checkCategoria(theString));
                        }

                        /* Setto il link all'articolo */
                        else if ("link".equals(nodeName)) {
                            item.setLink(theString);
                        }
                    }
                }
                Log.e("Notizia", item.getTitolo());
                y++;

                /* Aggiungi l'item alla lista degli item */
                feed.addItem(item);
            }

        } catch (RuntimeException e) {
        Log.e("RuntimeException: ", Log.getStackTraceString(e));
    } catch (Exception e) {
        Log.e("Exception: ", e.getMessage());
    } 



        /* Ritorna il feed popolato */
        return feed;
    }       
}

感谢帮助!

编辑:我改变了捕获,现在我得到了

02-20 19:42:48.653: E/RuntimeException:(6414): java.lang.StringIndexOutOfBoundsException: length=1528; index=-1
02-20 19:42:48.653: E/RuntimeException:(6414):  at java.lang.String.indexAndLength(String.java:584)
02-20 19:42:48.653: E/RuntimeException:(6414):  at java.lang.String.substring(String.java:1449)
02-20 19:42:48.653: E/RuntimeException:(6414):  at com.example.immoderati.parse.Parser.parseXml(Parser.java:85)
02-20 19:42:48.653: E/RuntimeException:(6414):  at com.example.immoderati.HomeActivity$AsyncLoadXMLFeed.doInBackground(HomeActivity.java:114)
02-20 19:42:48.653: E/RuntimeException:(6414):  at com.example.immoderati.HomeActivity$AsyncLoadXMLFeed.doInBackground(HomeActivity.java:1)
02-20 19:42:48.653: E/RuntimeException:(6414):  at android.os.AsyncTask.call(AsyncTask.java:288)
02-20 19:42:48.653: E/RuntimeException:(6414):  at java.util.concurrent.FutureTask.run(FutureTask.java:237)
02-20 19:42:48.653: E/RuntimeException:(6414):  at android.os.AsyncTask$SerialExecutor.run(AsyncTask.java:231)
02-20 19:42:48.653: E/RuntimeException:(6414):  at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
02-20 19:42:48.653: E/RuntimeException:(6414):  at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
02-20 19:42:48.653: E/RuntimeException:(6414):  at java.lang.Thread.run(Thread.java:841)

清理文本的方法有误。感谢 printstacktrace 找到它,感谢@Selvin