【问题标题】:Android feed.addItem() doesn't add last itemAndroid feed.addItem() 不添加最后一项
【发布时间】:2015-02-20 17:42:50
【问题描述】:

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

【问题讨论】:

  • 因为这样的代码:catch (Exception e) { /*yum, tasty exception, i will eat it and do not share with anyone, not even log it nor print on screen*/ } 你永远不会知道为什么...
  • 嗨,我改成 catch (RuntimeException e) { Log.e("RuntimeException: ", e.getMessage()); } catch (Exception e) { Log.e("Exception: ", e.getMessage());我得到 02-20 19:16:12.873: E/RuntimeException:(31816): length=1528;索引=-1
  • 使用 e.printstacktrace() 并将日志添加到问题中
  • 解决了,清理文本的方法有错误。感谢 printstacktrace 找到它,感谢@Selvin
  • 如果解决了,您可以将解决方案放入提供的答案框中,然后接受。这比将“已解决”一词编辑到问题中更可取!

标签: android vector jsoup feedparser nodelist


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-09-22
    • 1970-01-01
    • 2017-07-10
    相关资源
    最近更新 更多