2012-10-12 66 views
1

我想读从URL一个XML文档:如何从URL读取XML在Android的

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(); 
    } 

我得到一个错误异常

android.os.NetworkOnMainThreadException

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

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

搜索。这个问题到处都有数百个答案。 – njzk2

回答

0

为什么你没有Google或查找错误这里在stackoverflow?它充满了答案...

您必须扩展一个AsyncTask以避免对GUI的阻塞,并在后台执行这种操作(如下载或解析东西)。

2

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

词根记忆HTTP请求从Web服务 2.Parse一个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); 
    }