UnknownHostException in java (that too only sometimes)
        Posted  
        
            by Nitesh Panchal
        on Stack Overflow
        
        See other posts from Stack Overflow
        
            or by Nitesh Panchal
        
        
        
        Published on 2010-05-25T17:02:44Z
        Indexed on 
            2010/05/25
            17:11 UTC
        
        
        Read the original article
        Hit count: 181
        
dns
Hello,
I am trying to read rss feed of Yahoo but i am unable to make it work properly. The code is absolutely correct , i am sure about it. It works sometimes but sometimes i get UnknownHostException. What can be the reason? Is there some problem with my internet or something else? This is my code :-
 public List<RssFeed> getRssFeed() {
        try {
            List<RssFeed> objList = new ArrayList<RssFeed>();
            DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
            DocumentBuilder db = dbf.newDocumentBuilder();
            Document doc = db.parse("http://rss.news.yahoo.com/rss/india");
            //doc.getDocumentElement().normalize();
            Element docElement = doc.getDocumentElement();
            NodeList objChannelList = docElement.getChildNodes();
            for (int intIndex = 0; intIndex < objChannelList.getLength(); intIndex++) {
                if (objChannelList.item(intIndex).getNodeType() == Node.ELEMENT_NODE) {
                    Element elemItem = (Element) objChannelList.item(intIndex);
                    NodeList itemList = elemItem.getElementsByTagName("item");
                    //show only 3 news
                    int count = itemList.getLength() > 3 ? 3 : objChannelList.getLength();
                    for (int intSubIndex = 0; intSubIndex < count; intSubIndex++) {
                        NodeList itemDetailList = itemList.item(intSubIndex).getChildNodes();
                        String strTitle = ((Node) itemDetailList.item(RSS_VALUES.TITLE.getValue())).getFirstChild().getNodeValue();
                        String strdescription = ((Node) itemDetailList.item(RSS_VALUES.DESCRIPTION.getValue())).getFirstChild().getNodeValue();
                        String strLink = ((Node) itemDetailList.item(RSS_VALUES.LINK.getValue())).getFirstChild().getNodeValue();
                        //System.out.println(strTitle + "\n" + strdescription + "\n" + strLink + "\n\n\n\n");
                        objList.add(new RssFeed(strTitle, strdescription, strLink));
                    }
                }
            }
            return objList;
        } catch (SAXException ex) {
            Logger.getLogger(Utils.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(Utils.class.getName()).log(Level.SEVERE, null, ex);
        } catch (ParserConfigurationException ex) {
            Logger.getLogger(Utils.class.getName()).log(Level.SEVERE, null, ex);
        }
        return null;
    }
Thanks in advance :). This problem has been bugging me since 1 month. Don't know why does Java in this case behave as per its mood :(
© Stack Overflow or respective owner