如何设置请求超时异常

时间:2012-09-19 10:36:18

标签: android

这是我的代码,下面从服务器获取xml响应如何在此代码中设置请求超时计时器?我的应用程序返回异常,如果服务器没有响应hw我在下面的代码中设置计时器,所以如果服务器在15秒异常调用中没有响应

public static   String[][] AgAppXMLParser( String parUrl) {



    String _node,_element;
    String[][] xmlRespone = null;
    try {

            String url = www.xyz.com;
            URL finalUrl = new URL(url);    


            DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
            DocumentBuilder db = dbf.newDocumentBuilder();
            Document doc = db.parse(new InputSource(finalUrl.openStream()));
            doc.getDocumentElement().normalize();

            NodeList list=doc.getElementsByTagName("*");
            _node=new String();
            _element = new String();
            xmlRespone = new String[list.getLength()][2];

            //this "for" loop is used to parse through the
            //XML document and extract all elements and their
            //value, so they can be displayed on the device

            for (int i=0;i<list.getLength();i++)
                {
                    Node value=list.item(i).      getChildNodes().item(0);
                    _node=list.item(i).getNodeName();
                    _element=value.getNodeValue();
                    xmlRespone[i][0] = _node;
                    xmlRespone[i][1] = _element;

                }//end for

        }//end try











    catch (Exception e)
    {
      flag=false;
      Log.e(LOG_TAG, "CONNECTION ERROR   SERVER NOT RESPONDING", e);
    } 



    return xmlRespone;         

}

1 个答案:

答案 0 :(得分:0)

为此,您需要实施计时器 ...请参阅以下参考链接...

How to set a timer in android

Android: how to use a timer

Timer in Android

希望这会对你有帮助......