-1

我现在正在做一个项目,我的 XMLParser.java 使应用程序强制在 Android 4.0 或更高版本的设备上关闭。我知道我应该实现 Async 但我不知道如何实现。有人可以帮助我吗?这是XMLParser.java的代码:

import *stuff


public class XMLParser {

    public XMLParser() {

    }


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

        try {
        // defaultHttpClient
        DefaultHttpClient httpClient = new DefaultHttpClient();
        HttpGet httpPost = new HttpGet(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

您可以使用droidQuery库来执行您尝试执行的所有操作(没有任何错误):

$.ajax(new AjaxOptions(url).context(this)
                           .type("GET")
                           .dataType("XML")
                           .success(new Function() {
                               @Override
                               public void invoke($ droidQuery, Object... params) {
                                   Document xml = (Document) params[0];
                                   //TODO parse XML Document
                               }
                           })
                           .error(new Function() {
                               @Override
                               public void invoke($ droidQuery, Object... params) {
                                   droidQuery.alert("ERROR: " + (String) params[2]);
                               }
                           }));
于 2013-07-17T14:21:59.340 回答