Java中的UnknownHostException(也只是有时) [英] UnknownHostException in java (that too only sometimes)

查看:475
本文介绍了Java中的UnknownHostException(也只是有时)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试阅读雅虎的RSS Feed,但我无法使其正常工作。代码是绝对正确的,我相信它。它有时工作,但有时我会收到UnknownHostException。可能是什么原因?我的互联网还有什么问题吗?这是我的代码: -

  public List< RssFeed> getRssFeed(){

try {

列表< RssFeed> objList = new ArrayList< RssFeed>();


DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
文档doc = db.parse(http://rss.news.yahoo.com/rss/india);

//doc.getDocumentElement()。normalize();
元素docElement = doc.getDocumentElement();
NodeList objChannelList = docElement.getChildNodes(); (int intIndex = 0; intIndex< objChannelList.getLength(); intIndex ++){
if(objChannelList.item(intIndex).getNodeType()== Node .ELEMENT_NODE){


元素elemItem =(Element)objChannelList.item(intIndex);
NodeList itemList = elemItem.getElementsByTagName(item);

//仅显示3个新闻
int count = itemList.getLength()> 3? 3:objChannelList.getLength(); (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 +\\\
+ strdescription +\\\
+ strLink +\\\
\\\
\\\
\\\
);
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);
}

返回null;
}

提前感谢:)。自1个月以来,这个问题一直在追踪我。不知道为什么Java在这种情况下会像其心情一样行事:(

解决方案

如果DNS中的打嗝可能会发生服务器已经发生,除了使DNS服务器更强壮还是寻找另一个,您也可以使用完整的IP地址而不是主机名,这样就不需要根据主机名查找IP地址。罢工>到目前为止,它是 216.115.98.240

  db.parse (http://216.115.98.240/rss/india); 

,我宁愿修复DNS问题,并喜欢DNS,因为IP地址可能会不时更改。



更新:IP地址方法显然不适用于雅虎新闻RSS Feed域,您应该真正使用主机名,并修复DNS服务器问题。


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

解决方案

This may occur if a hiccup in DNS server has occurred. Apart from making the DNS server more robust or looking for another one, you can also just use the full IP address instead of the hostname. This way it doesn't need to lookup the IP address based on the hostname. As of now it's 216.115.98.240.

db.parse("http://216.115.98.240/rss/india");

However, I would rather fix the DNS issue and prefer the DNS since IP addresses may change from time to time.

Update: the IP address approach apparently doesn't work for Yahoo News RSS feed domain. You should then really use the hostname instead and fix the DNS server issue.

这篇关于Java中的UnknownHostException(也只是有时)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆