从url获取null xml

时间:2014-01-13 12:16:23

标签: android xml web-services http

我试图从网址获取xml输出,但我得到一个null xml作为回报。但是,如果我尝试在浏览器上执行该URL,它会让我回复...这是我的代码

    signup.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View arg0) {
            String us = username.getText().toString();
            String na = name.getText().toString();
            String pa = password.getText().toString();
            String rep = repass.getText().toString();
            String e = email.getText().toString();

            if (us.equals("") || na.equals("") || pa.equals("")
                    || rep.equals("") || e.equals("")) {
                Toast.makeText(getApplicationContext(), "Fill All Entries",
                        Toast.LENGTH_SHORT).show();
            } else {
                if (pa.equals(rep)) {
                    // xml_ajax_register(us, na, pa, e);
                    String url = "http://localhost:9090/plugins/userService/userservice?type="
                            + "add&secret=0LRF2i8I&username="
                            + us
                            + "&password="
                            + pa
                            + "&name="
                            + na
                            + "&email="
                            + e;
                    System.out.println("URL IS "+url);
                    String xml = parser.getXmlFromUrl(url);
                    // Document doc=parser.getDomElement(xml);
                    //
                    // NodeList nl=doc.getElementsByTagName("result");

                    System.out.println("XML Is " + xml);

                } else {
                    Toast.makeText(getApplicationContext(),
                            "PassWord Doesn't Match", Toast.LENGTH_SHORT)
                            .show();

                }

            }

            // String xml = getXmlFromUrl(url);

        }

    });

我的xml解析器类是

    public class XMLParser {

// constructor
public XMLParser() {

}

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

    try {
        // defaultHttpClient
        DefaultHttpClient httpClient = new DefaultHttpClient();
        HttpGet httpGet = new HttpGet(url);

        HttpResponse httpResponse = httpClient.execute(httpGet);
        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));
    }

提前致谢!!

0 个答案:

没有答案