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



我不明白为什么我的代码没有将所有10项添加到我的提要中,而只添加了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$2.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$1.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)

清理文本的方法中出现错误。感谢printstackrace,感谢@Selvin

最新更新