SG87 SG87 - 1 year ago 40
Android Question

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

I don't understand why my code doesn't add all the 10 items to my feed, but just 9!

The cycle explores all the 10 items, but doesn't add the last one, I checked with Log.e.

Here's the code:

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;
}
}


Thanks for help!

EDIT: I changed the catch, now I get

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)

Answer Source

there was an error in the method that cleaned the text. Found it thanks to printstacktrace, thanks @Selvin

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download