Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/kotlin/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在Android中从URL读取XML_Android_Xml_Httpurlconnection - Fatal编程技术网

如何在Android中从URL读取XML

如何在Android中从URL读取XML,android,xml,httpurlconnection,Android,Xml,Httpurlconnection,我想从以下位置读取XML文档: 我得到一个错误异常 android.os.NetworkOnMainThreadException 我在清单文件中添加了使用权限: <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> 这不是XML问题,而是严格的模式问

我想从以下位置读取XML文档:

我得到一个错误异常

android.os.NetworkOnMainThreadException

我在清单文件中添加了使用权限:

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

这不是XML问题,而是严格的模式问题。 你不应该在Gui线程中做时间密集型的事情,而应该在自己的线程中做

但是,您可以禁用它,但不应禁用;)
这不是XML问题,而是严格的模式问题。 你不应该在Gui线程中做时间密集型的事情,而应该在自己的线程中做

但是,您可以禁用它,但不应禁用;)
为什么不在stackoverflow上搜索或查找错误?它充满了答案


您必须扩展AsyncTask以避免GUI阻塞,并在后台执行此类操作(如下载或解析内容)。

为什么不在stackoverflow上搜索或查找错误?它充满了答案


您必须扩展AsyncTask以避免GUI阻塞,并在后台执行此类操作(如下载或解析内容)。

从服务器读取数据有两个步骤

1.发出HTTP请求以从Web服务获取数据 2.解析XML文档并读取内容

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);
    }
试试看
{
URL=新URL(“http://www.w3schools.com/xml/note.xml");
DocumentBuilderFactory dbf=DocumentBuilderFactory.newInstance();
DocumentBuilder db=dbf.newDocumentBuilder();
Document doc=db.parse(新的输入源(url.openStream());
doc.getDocumentElement().normalize();
NodeList NodeList=doc.getElementsByTagName(“注释”);
/**按arraylist大小指定textview数组长度*/
to=新文本视图[nodeList.getLength()];
from=newtextView[nodeList.getLength()];
heading=newtextView[nodeList.getLength()];
body=newtextView[nodeList.getLength()];
for(int i=0;i
从服务器读取数据有两个步骤

1.发出HTTP请求以从Web服务获取数据 2.解析XML文档并读取内容

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);
    }
试试看
{
URL=新URL(“http://www.w3schools.com/xml/note.xml");
DocumentBuilderFactory dbf=DocumentBuilderFactory.newInstance();
DocumentBuilder db=dbf.newDocumentBuilder();
Document doc=db.parse(新的输入源(url.openStream());
doc.getDocumentElement().normalize();
NodeList NodeList=doc.getElementsByTagName(“注释”);
/**按arraylist大小指定textview数组长度*/
to=新文本视图[nodeList.getLength()];
from=newtextView[nodeList.getLength()];
heading=newtextView[nodeList.getLength()];
body=newtextView[nodeList.getLength()];
for(int i=0;i