Android AlertDialog 问题

标签 android xml-parsing android-alertdialog httpresponse

我想让我的应用程序在 Http Response 等于 NULL 时显示一个对话框。但无法找到一种方法来做到这一点。我已经在我的代码中标记了它。谁能告诉我它是怎么做到的?以下是我的代码和我的尝试。

public class XMLParser {
    private Activity activity = null;
    // constructor
    public XMLParser(Activity act) {
        activity = act;
    }

    /**
     * Getting XML from URL making HTTP request
     * @param url string
     * */
    public String getXmlFromUrl(String url) {
        String xml = null;

        try {
            // defaultHttpClient
            DefaultHttpClient httpClient = new DefaultHttpClient();
            HttpPost httpPost = new HttpPost(url);
            HttpResponse httpResponse = httpClient.execute(httpPost);

            if (httpResponse == null) {

                AlertDialog.Builder builder = new AlertDialog.Builder(activity);
                 builder.setMessage("No Response from Server ")
                        .setCancelable(false)
                        .setPositiveButton("Exit", new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int id) {
                                System.exit(0);
                            }

                           });
                AlertDialog alert = builder.create();
                 alert.show(); 

            }
            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;
        }

        /**
         * Getting XML DOM element
         * @param XML string
         * */
        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;
        }

        /** Getting node value
          * @param elem element
          */
         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 "";
         }

         /**
          * Getting node value
          * @param Element node
          * @param key string
          * */
         public String getValue(Element item, String str) {     
                NodeList n = item.getElementsByTagName(str);        
                return this.getElementValue(n.item(0));
            }
    }

最佳答案

初始化AlertDialog 对象时需要Activity 上下文。因此,按如下方式稍微更改您的类文件:

private Activity activity = null;

public XMLParser(Activity act) {
   activity = act;
}

稍后在使用 AlertDialog 时,将其初始化如下:

AlertDialog.Builder builder = new AlertDialog.Builder(activity);

关于Android AlertDialog 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12129332/

相关文章:

android - 以非零退出值完成 2 BaseGameServices 和 Admob

c# - 如何在 iOS 上打开下载的文件

android - 确定使用警报对话框(单选按钮)所做的选择

Android - 更改首选项时确认对话框

android - 刷新 Activity/"performing stop of activity that is not resumed"错误

java - Android 中意外的 StreamTokenizer 行为

swift - Swift 中的 Codable 和 XMLParser

go - Xml 解码失败

java - Android XML解析: Parse this special Type of XML i have no Idia about this

android - 如何在底部滑动后继续阅读ListView中的内容?