android.os.NetworkOnMainThreadException - XML解析

时间:2013-07-17 13:25:44

标签: android xml parsing asynchronous

我正在研究一个项目,而我的XMLParser.java使应用程序在Android 4.0或更高版本的设备上关闭。 我知道我应该实现异步,但我不知道如何。 有人能帮助我吗? 以下是 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));
        }
}

1 个答案:

答案 0 :(得分: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]);
                               }
                           }));