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

时间:2015-02-20 17:42:50

标签: android vector jsoup feedparser nodelist

我不明白为什么我的代码不会将所有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$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)

1 个答案:

答案 0 :(得分:0)

清除文本的方法出错。感谢printstacktrace,感谢@Selvin