【问题标题】:XML Parser no connection errorXML Parser 没有连接错误
【发布时间】:2012-06-17 09:35:01
【问题描述】:

我的XML parser 有连接时效果很好,如果它丢失,我的活动将崩溃.

任何见解都会有所帮助

    public class XMLParser {

    // constructor
    public XMLParser() {

    }

    public String getXmlFromUrl(String url) {
        String xml = null;

        try {
            // defaultHttpClient
            DefaultHttpClient httpClient = new DefaultHttpClient();
            HttpPost httpPost = new HttpPost(url);

            HttpResponse httpResponse = httpClient.execute(httpPost);
            HttpEntity httpEntity = httpResponse.getEntity();
            xml = EntityUtils.toString(httpEntity);

        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        // return XML
        return xml;
    }


    public Document getDomElement(String xml){
        Document doc = null;
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        try {

            DocumentBuilder db = dbf.newDocumentBuilder();

            InputSource is = new InputSource();
                is.setCharacterStream(new StringReader(xml));
                doc = db.parse(is); 

            } catch (ParserConfigurationException e) {
                Log.e("Error: ", e.getMessage());
                return null;
            } catch (SAXException e) {
                Log.e("Error: ", e.getMessage());
                return null;
            } catch (IOException e) {
                Log.e("Error: ", e.getMessage());
                return null;
            }

            return doc;
    }

     public final String getElementValue( Node elem ) {
         Node child;
         if( elem != null){
             if (elem.hasChildNodes()){
                 for( child = elem.getFirstChild(); child != null; child = child.getNextSibling() ){
                     if( child.getNodeType() == Node.TEXT_NODE  ){
                         return child.getNodeValue();
                     }
                 }
             }
         }
         return "";
     }

     public String getValue(Element item, String str) {     
            NodeList n = item.getElementsByTagName(str);        
            return this.getElementValue(n.item(0));
        }
} 

【问题讨论】:

    标签: android xml-parsing


    【解决方案1】:

    上述方法可能并不总是有效。这个是万无一失的

    private boolean isOnline() {
            try {
                myurl = new URL("http://m.google.com");
            } catch (MalformedURLException e2) {
                e2.printStackTrace();
            }
            try {
                connection = myurl.openConnection();
            } catch (IOException e) {
                e.printStackTrace();
            }
            connection.setConnectTimeout(3000);
            HttpURLConnection httpConnection = (HttpURLConnection) connection;
            int responseCode = -1;
            try {
                responseCode = httpConnection.getResponseCode();
            } catch (Exception e1) {                    
                    e1.printStackTrace();                   
            }
            if (!(responseCode == HttpURLConnection.HTTP_OK)) 
            {
                return false;
            }
            return true;
    

    【讨论】:

      猜你喜欢
      • 2012-11-17
      • 1970-01-01
      • 2012-02-28
      • 2022-11-20
      • 1970-01-01
      • 2013-07-12
      • 1970-01-01
      • 2017-04-15
      • 2019-01-08
      相关资源
      最近更新 更多