java - xmlreader do not run the startelement method -
i have problems xmlreader. can run , url correct have not run startelement method. value returned null. want know why happen , soludtion. thank you!
package com.headfirstlabs.nasadailyimage; import java.io.ioexception; import java.io.inputstream; import java.net.httpurlconnection; import java.net.url; import java.net.unknownhostexception; import java.util.jar.attributes; import javax.xml.parsers.saxparser; import javax.xml.parsers.saxparserfactory; import org.xml.sax.inputsource; import org.xml.sax.saxexception; import org.xml.sax.xmlreader; import org.xml.sax.helpers.defaulthandler; import android.graphics.bitmap; import android.graphics.bitmapfactory; import android.os.strictmode; public class iotdhandler extends defaulthandler { private string url = "http://www.nasa.gov/rss/image_of_the_day.rss"; private boolean inurl = false; private boolean intitle = false; private boolean indescription = false; private boolean initem = false; private boolean indate = false; private bitmap image = null; private string title = null; private stringbuffer description = new stringbuffer(); private string date = null; public void processfeed() { try { strictmode.threadpolicy policy = new strictmode.threadpolicy.builder().permitall().build(); strictmode.setthreadpolicy(policy); saxparserfactory factory = saxparserfactory.newinstance(); saxparser parser = factory.newsaxparser(); xmlreader reader = parser.getxmlreader(); reader.setcontenthandler(this); inputstream inputstream = new url(url).openstream(); reader.parse(new inputsource(inputstream)); } catch(unknownhostexception e) { title="unknownhostexception"; } catch(ioexception e) { title="ioexception"; } catch(saxexception e) { title="saxexception"; } catch (exception e) { title="exception"; system.out.println(e); } } private bitmap getbitmap(string url) { try { httpurlconnection connection = (httpurlconnection)new url(url).openconnection(); connection.setdoinput(true); connection.connect(); inputstream input = connection.getinputstream(); bitmap bitmap = bitmapfactory.decodestream(input); input.close(); return bitmap; } catch (ioexception ioe) { return null; } } public void startelement(string uri, string localname, string qname, attributes attributes) throws saxexception { if (localname.equals("url")) { inurl = true; } else { inurl = false; } if (localname.startswith("item")) { initem = true; } else if (initem) { if (localname.equals("title")) { intitle = true; } else { intitle = false; } if (localname.equals("description")) { indescription = true; } else { indescription = false; } if (localname.equals("pubdate")) { indate = true; } else { indate = false; } } } public void characters(char ch[], int start, int length) { string chars = new string(ch).substring(start, start + length); if (inurl && url == null) { image = getbitmap(chars); } if (intitle && title == null) { title = chars; } if (indescription) { description.append(chars); } if (indate && date == null) { date = chars; } } public bitmap getimage() { return image; } public string gettitle() { return title; } public stringbuffer getdescription() { return description; } public string getdate() { return date; } }
the problem line:
import java.util.jar.attributes;
it should
import org.xml.sax.attributes;
because you've got wrong type fourth parameter of startelement
, method doesn't override startelement
in defaulthandler
, , default implementation of startelement
in defaulthandler
being called instead of method.
you can use @override
annotation indicate method should override method in superclass. if method @override
annotation not override superclass method, compiler error. in fact, such error if put annotation on startelement
method.
Comments
Post a Comment