未处理的异常:org.xml.sax.SAXException

时间:2018-07-05 14:32:48

标签: java android

我正在阅读RSS新闻,并希望检查呼叫的应用程序是否已被取消/停止(例如设备旋转)。大多数RSS feed中都有一个循环,该循环允许循环中的语句检查isCancelled()。我使用的是所谓的简化SAX阅读。没有循环。我称之为解析,并具有用于不同项目的处理程序。我有其中之一,(结束元素)检查isCancelled()。 我在红色带下划线的throw语句上收到“未处理的异常:org.xml.sax.SAXException”。它不会编译。 我尝试了尽可能多的组合,以考虑try / catch语句的去向,然后放入org.xml.sax。 我想我有一个选择是使用具有循环的RSS feed读取选项之一,但是如果可能的话,我想使用这种简化的SAX读取,因为它应该是最有效的。

public ArrayList<FeedItem> GetWithSimplifiedSax(String theUrl)
    throws SAXException { 
try {
    url= new URL(theUrl);
    HttpURLConnection connection = (HttpURLConnection) url.openConnection();
    connection.setRequestMethod("GET");
    InputStream inputStream = connection.getInputStream();

    final FeedItem currentItem = new FeedItem(); 
    RootElement root = new RootElement("rss");
    final ArrayList<FeedItem> feedItems = new ArrayList<>();
    android.sax.Element channel = root.getChild("channel");
    android.sax.Element item = channel.getChild("item");

    item.setEndElementListener(new EndElementListener(){
        public void end() {
            feedItems.add(currentItem.myCopy());
            if(isCancelled()){ 
                throw new SAXException("cancel");
             //<<DOES NOT LIKE ABOVE LINE
             // *** Gives unhandled exception:org.xml.sax.SAXException
            }
        }
    });
    item.getChild("title").setEndTextElementListener(new EndTextElementListener(){
        public void end(String body) {
            currentItem.setTitle(body); 
        }
    });
    item.getChild("link").setEndTextElementListener(new EndTextElementListener(){
        public void end(String body) {
            currentItem.setLink(body);
        }
    });
    item.getChild("description").setEndTextElementListener(new EndTextElementListener(){
        public void end(String body) {
            currentItem.setDescription(body);
        }
    });
    item.getChild("pubdate").setEndTextElementListener(new EndTextElementListener(){
        public void end(String body) {
            currentItem.setPubDate(body);
        }
    });


    try {
        Xml.parse(inputStream, Xml.Encoding.UTF_8, root.getContentHandler());
    } catch(SAXException e){
        Log.e("SAX", e.getMessage());
        return null;
    }
    return feedItems;
} catch ( IOException  e) {
    Log.e("MYERROR", e.getMessage());
    //e.printStackTrace();
    return null;
} //catch (org.xml.sax.SAXException e) {
  //  Log.e("SAX", e.getMessage());
   // //e.printStackTrace();
  //  return null;
//} //try catch

} //方法:公共...简化的SAX

0 个答案:

没有答案