2012-08-26 42 views
1

我想讓我的應用程序顯示一個對話框,當Http響應等於NULL。但無法找到辦法。我在代碼中標記了它。任何人都可以請告訴我它是如何完成的?以下是我的代碼和我的嘗試。Android AlertDialog問題

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

回答

1

您初始化AlertDialog對象時需要Activity上下文。因此,改變你的類文件,如下一點:使用AlertDialog

private Activity activity = null; 

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

後來,如下初始化:

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