tags:

views:

135

answers:

2

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 :(

A: 
BalusC
Thanks BalusC :). Great, you gave the ip. Now the DNS itself doesn't come into picture. But it's not working. Now i get this error :- java.io.FileNotFoundException: http://216.115.98.240/rss/india
Ankit Rathod
Even when i type "http://216.115.98.240/rss/india" i get nothing. Is this ip correct? When i did ping i got this ip 216.115.97.236. However in this also i get the same exception in java and in browser also nothing displays.
Ankit Rathod
+2  A: 

An UnknownHostException indicates the host specified couldn't be translated to an IP address. It could very well be a problem with your DNS server.

Mark Peters
Ok MarkPeters, so how should i resolve this? since i have to submit my work in college and i don't think i will able to have access to college's router and change the dns server in it :(
Ankit Rathod
@Nitesh: Is it failing in the college's environment or only in your local environment? If it's inconsistently failing from the college's environment, I would both notify your teacher or tutor that there may be an environment problem that's out of your control, and also try to lobby your college IT staff to diagnose and fix the problem.You might consider an approach where you catch the exception and try again a few times, perhaps a few seconds/minutes apart.
Mark Peters