2011-04-25 178 views
0

我解析從一個URL的XML。該網址有移動IMEI號和基於我的應用程序的搜索字符串。我把我的XML解析代碼在Android項目中不起作用。但如果我作爲單獨的Java程序運行它正在工作。請幫幫我。解析不工作在android

Log.e("rsport-", "function1"); 
try{ 
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); 
    factory.setIgnoringComments(true); 
    factory.setCoalescing(true); // Convert CDATA to Text nodes 
    factory.setNamespaceAware(false); // No namespaces: this is default 
    factory.setValidating(false); // Don't validate DTD: also default 
    DocumentBuilder parser = factory.newDocumentBuilder(); 
    Log.e("rsport-", "function2"); 
    Document document = parser.parse("http://demo.greatinnovus.com/restingspot/search?userid=xxxxxxxxxxxxxxxx&firstname=a&lastname=a"); 
    Log.e("rsport-","function3"); 
    NodeList sections = document.getElementsByTagName("Searchdata"); 
    int numSections = sections.getLength(); 
     for (int i = 0; i < numSections; i++) 
    { 
     Element section = (Element) sections.item(i); 
     if(section.hasChildNodes()==true){ 
      NodeList section1=section.getChildNodes(); 
       for(int j=0;j<section1.getLength();j++){ 
       if(section1.item(j).hasChildNodes()==true) { 
        for(int k=0;k<section1.item(j).getChildNodes().getLength();k++)      
           xmlvalue=String.valueOf(section1.item(j).getChildNodes().item(k).getNodeValue()).trim(); 

           arl.add(xmlvalue); 
        } 
       } 
      } 
     } 

    } 
     } 
    catch(Exception e){} 
     System.out.println("id"+id+"  searchdatacount"+searchdatacount); 
     System.out.println("---------"); 
     ListIterator<String> litr = arl.listIterator(); 
     while (litr.hasNext()) { 
      String element = litr.next(); 
      Log.e("rsport-", "elememt"); 
     } 

之後的Log.e("rsport-", "function2");不起作用。

+1

是否拋出異常?什麼是logcat輸出? – 2011-04-25 13:23:35

+1

告訴我你在Logcat中發生了什麼錯誤以及你想解析的xml中的哪個標記 – 2011-04-25 13:24:38

+0

它顯示異常是InputSource需要流或者讀取器 logcat 04-25 19:01:22.443:ERROR/rsport(2177) :{chap = hjdasgdas} 04-25 19:01:22.453:錯誤/ listview(2177):[email protected] 04-25 19:01:22.563:ERROR/rsport-(2177):function1 04-25 19:01:22.563:ERROR/rsport-(2177):function2 04-25 19:01:22.583:ERROR/rsport ---(2177):InputSource需要流或讀取器 – 2011-04-25 13:32:50

回答

0

請參閱我的博客,我已經給出了詳細解釋,http://sankarganesh-info-exchange.blogspot.com/2011/04/parsing-data-from-internet-and-creating.html,並確保您已在您的Manifest文件中添加Internet權限。

如果您已通過Myblog走了,那麼你就能夠發現你做下面一行是錯誤

Document document = parser.parse("http://demo.greatinnovus.com/restingspot/search?userid=xxxxxxxxxxxxxxxx&firstname=a&lastname=a"); 

使用這樣

URL url =new URL("http://demo.greatinnovus.com/restingspot/search?userid=xxxxxxxxxxxxxxxx&firstname=a&lastname=a"); 
Document document= parser.parse(new InputSource(url.openStream())); 
+0

hi sangar ganesh i在Logcat中獲得以下內容: 它顯示異常是InputSource需要流或讀取器logcat 04-25 19:01:22.443:ERROR/rsport(2177):{chap = hjdasgdas } 04-25 19:01:22.453:ERROR/listview(2177):[email protected] 04-25 19:01:22.563:ERROR/rsport-(2177):function1 04-25 19:01:22.563 :錯誤/ rsport-(2177):function2 04-25 19:01:22.583:錯誤/ rsport ---(2177):InputSource需要流或讀取器 – 2011-04-25 13:35:43

+0

感謝shangar它工作 – 2011-04-25 13:51:41