3

接続が失われた場合、アクティビティがクラッシュ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));
        }
} 
4

1 に答える 1

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-06-17T12:21:42.327 に答える