How to reading XML from a URL in Android

夙愿已清 提交于 2019-12-20 04:18:08

问题


I want to read a XML document from a URL:

public void DownloadXmlFile() throws IOException{
        //TODO
        String url = "http://api.m1858.com/coursebook.xml";
        URL u = new URL(url);
        HttpURLConnection conn = (HttpURLConnection) u.openConnection();
        conn.setReadTimeout(10000);
        conn.setConnectTimeout(15000);
        conn.setRequestMethod("GET");
        conn.setDoInput(true);
        conn.connect();
    }

I get an Error Exception

android.os.NetworkOnMainThreadException

I added uses-permission in manifest file:

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

回答1:


This is not an XML Problem its a Strict Mode Problem. You should'nt do time intensiv things in Gui Thread, do it in a own Thread.

  • Blogpost with introduction
  • Developers API infos
  • BestPracties

However, you can disable it, but you shouldt ;) see here




回答2:


there are two step for read data from server...

1.Make a HTTP request to get the data from the webservice 2.Parse a XML document and read the contents

try 
    {

        URL url = new URL("http://www.w3schools.com/xml/note.xml");

        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        DocumentBuilder db = dbf.newDocumentBuilder();
        Document doc = db.parse(new InputSource(url.openStream()));
        doc.getDocumentElement().normalize();

        NodeList nodeList = doc.getElementsByTagName("note");
        /** Assign textview array lenght by arraylist size */


        to = new TextView[nodeList.getLength()];
        from = new TextView[nodeList.getLength()];
        heading = new TextView[nodeList.getLength()];
        body = new TextView[nodeList.getLength()];



        for (int i = 0; i < nodeList.getLength(); i++) 
        {
            Node node = nodeList.item(i);

            to[i] = new TextView(this);
            from[i] = new TextView(this);
            body[i] = new TextView(this);
            heading[i] = new TextView(this);

            Element fstElmnt = (Element) node;
            NodeList toList = fstElmnt.getElementsByTagName("to");
            Element nameElement = (Element) toList.item(0);
            toList = nameElement.getChildNodes();
            to[i].setText("To = "+ ((Node) toList.item(0)).getNodeValue());

            NodeList fromList = fstElmnt.getElementsByTagName("from");
            Element fromElement = (Element) fromList.item(0);
            fromList = fromElement.getChildNodes();
            from[i].setText("from = "+ ((Node) fromList.item(0)).getNodeValue());

            NodeList headingList = fstElmnt.getElementsByTagName("heading");
            Element headingElement = (Element) headingList.item(0);
            headingList = headingElement.getChildNodes();
            heading[i].setText("heading = "+ ((Node) headingList.item(0)).getNodeValue());


            NodeList bodyList = fstElmnt.getElementsByTagName("body");
            Element bodyElement = (Element) bodyList.item(0);
            bodyList = bodyElement.getChildNodes();
            body[i].setText("body = "+ ((Node) bodyList.item(0)).getNodeValue());

            layout.addView(to[i]);
            layout.addView(from[i]);
            layout.addView(heading[i]);
            layout.addView(body[i]);

        }
    } 
    catch (Exception e) 
    {
        System.out.println("XML Pasing Excpetion = " + e);
    }



回答3:


Why you don't google or look for the error here on stackoverflow? It's full of answers...

You have to extend an AsyncTask to avoid the blocking of the GUI and do this kind of operation (as downloading or parsing stuff) in background.



来源:https://stackoverflow.com/questions/12854650/how-to-reading-xml-from-a-url-in-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!