0

如何在此代码中设置请求超时?这是我的应用程序,如果 http 服务器没有响应或不可用的应用程序爆炸,它工作正常我如何在这个应用程序中设置请求超时?请帮我

public class AgAppHelperMethods   {

       private static final String LOG_TAG = null;

           private static AgAppHelperMethods instance = null;
public static   String[][] AgAppXMLParser( String parUrl) {



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

            String url = "www.xxxx.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;

                }

        }









 catch (Exception e)
 {

   Log.e(LOG_TAG, "Connection Error NET NOT WORKING", e);

 }
4

1 回答 1

2
ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
            NetworkInfo info = cm.getActiveNetworkInfo();
            if (info != null) {
                if (!info.isConnected()) {
                    Toast.makeText(this,
                            "Please check your internet connection and try again.",
                            Toast.LENGTH_LONG).show();

                }
                else {
//do your stuff
}
}

你也必须有这个权限

 <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
于 2012-09-14T12:33:31.147 回答