2012-05-18 76 views
-1

對於此給定的代碼,我收到一個異常。我不知道如何清除這一點。Android中的XML解析:獲取異常

package com.new_xml_parse; 
import java.net.URL; 
import javax.xml.parsers.DocumentBuilder; 
import javax.xml.parsers.DocumentBuilderFactory; 
import org.w3c.dom.Document; 
import org.w3c.dom.Element; 
import org.w3c.dom.Node; 
import org.w3c.dom.NodeList; 
import org.xml.sax.InputSource; 
import android.app.Activity; 
import android.os.Bundle; 
import android.widget.LinearLayout; 
import android.widget.TextView; 
public class New_xml_parseActivity extends Activity 
{ 
@Override 
public void onCreate(Bundle savedInstanceState) 
{ 
super.onCreate(savedInstanceState); 
/** Create a new layout to display the view */ 
LinearLayout layout = new LinearLayout(this); 
layout.setOrientation(1); 
/** Create a new textview array to display the results */ 
TextView name[]; 
TextView website[]; 
TextView category[]; 
try 
{ 
URL url = new URL("http://bestpropertyworld.com/MobileApp/xml1.xml"); 
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); 
DocumentBuilder db = dbf.newDocumentBuilder(); 
Document doc = db.parse(new InputSource(url.openStream())); 
doc.getDocumentElement().normalize(); 
NodeList nodeList = doc.getElementsByTagName("item"); 
/** Assign textview array lenght by arraylist size */ 
name = new TextView[nodeList.getLength()]; 
website = new TextView[nodeList.getLength()]; 
category = new TextView[nodeList.getLength()]; 
for (int i = 0; i < nodeList.getLength(); i++) 
{ 
Node node = nodeList.item(i); 
name[i] = new TextView(this); 
website[i] = new TextView(this); 
category[i] = new TextView(this); 
Element fstElmnt = (Element) node; 
NodeList nameList = fstElmnt.getElementsByTagName("name"); 
Element nameElement = (Element) nameList.item(0); 
nameList = nameElement.getChildNodes(); 
name[i].setText("Name = "+ ((Node) nameList.item(0)).getNodeValue()); 
NodeList websiteList = fstElmnt.getElementsByTagName("website"); 
Element websiteElement = (Element) websiteList.item(0); 
websiteList = websiteElement.getChildNodes(); 
website[i].setText("Website = "+ ((Node) websiteList.item(0)).getNodeValue()); 
category[i].setText("Website Category = "+ websiteElement.getAttribute("category")); 
layout.addView(name[i]); 
layout.addView(website[i]); 
layout.addView(category[i]); 
} 
} 
catch (Exception e) 
{ 
System.out.println("XML Pasing Excpetion = " + e); 
} 
/** Set the layout view to display */ 
setContentView(layout); 
} 
} 

在logcat中我收到這樣的,給給這個

05-18 15:16:00.132: W/WindowManager(77): Failure taking screenshot for (180x300) to layer 21010 
05-18 15:16:00.151: W/NetworkManagementSocketTagger(77): setKernelCountSet(10013, 1) failed with errno -2 
05-18 15:16:00.681: I/Process(77): Sending signal. PID: 187 SIG: 3 
05-18 15:16:00.698: I/dalvikvm(187): threadid=3: reacting to signal 3 
05-18 15:16:01.151: I/dalvikvm(187): Wrote stack traces to '/data/anr/traces.txt' 
05-18 15:16:02.121: W/NetworkManagementSocketTagger(77): setKernelCountSet(10041, 0) failed with errno -2 
05-18 15:16:02.902: D/dalvikvm(187): GREF has increased to 201 
05-18 15:16:03.573: D/dalvikvm(187): GC_FOR_ALLOC freed 594K, 6% free 11501K/12167K, paused 114ms 
05-18 15:16:03.601: I/dalvikvm-heap(187): Grow heap (frag case) to 12.399MB for 1157776-byte allocation 
05-18 15:16:03.851: D/dalvikvm(187): GC_CONCURRENT freed 23K, 6% free 12608K/13319K, paused 25ms+7ms 
05-18 15:16:04.087: D/dalvikvm(187): GC_FOR_ALLOC freed 1K, 6% free 12608K/13319K, paused 67ms 
05-18 15:16:04.101: I/dalvikvm-heap(187): Grow heap (frag case) to 13.524MB for 1203064-byte allocation 
05-18 15:16:04.251: D/dalvikvm(187): GC_CONCURRENT freed 0K, 6% free 13783K/14535K, paused 18ms+7ms 
05-18 15:16:04.491: D/dalvikvm(187): GC_FOR_ALLOC freed 4K, 6% free 13780K/14535K, paused 49ms 
05-18 15:16:04.501: I/dalvikvm-heap(187): Grow heap (frag case) to 14.625MB for 1157776-byte allocation 
05-18 15:16:04.561: D/dalvikvm(187): GC_FOR_ALLOC freed 0K, 5% free 14911K/15687K, paused 58ms 
05-18 15:16:05.971: D/dalvikvm(187): GC_FOR_ALLOC freed 56K, 4% free 16198K/16839K, paused 57ms 
05-18 15:16:06.411: D/dalvikvm(187): GC_FOR_ALLOC freed 16K, 4% free 17678K/18311K, paused 79ms 
05-18 15:16:07.231: D/dalvikvm(187): GC_CONCURRENT freed 614K, 4% free 19055K/19847K, paused 6ms+8ms 
05-18 15:16:07.391: I/ActivityManager(77): START {act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10200000 cmp=com.new_xml_parse/.New_xml_parseActivity} from pid 187 
05-18 15:16:07.391: W/WindowManager(77): Failure taking screenshot for (180x300) to layer 21005 
05-18 15:16:07.474: W/NetworkManagementSocketTagger(77): setKernelCountSet(10041, 1) failed with errno -2 
05-18 15:16:07.637: I/System.out(572): XML Pasing Excpetion = android.os.NetworkOnMainThreadException 
05-18 15:16:08.023: I/Process(77): Sending signal. PID: 572 SIG: 3 
05-18 15:16:08.031: I/dalvikvm(572): threadid=3: reacting to signal 3 
05-18 15:16:08.372: I/dalvikvm(572): Wrote stack traces to '/data/anr/traces.txt' 
05-18 15:16:08.591: I/Process(77): Sending signal. PID: 572 SIG: 3 
05-18 15:16:08.591: I/dalvikvm(572): threadid=3: reacting to signal 3 
05-18 15:16:08.611: I/dalvikvm(572): Wrote stack traces to '/data/anr/traces.txt' 
05-18 15:16:08.735: I/ActivityManager(77): Displayed com.new_xml_parse/.New_xml_parseActivity: +1s260ms 
05-18 15:16:09.191: W/NetworkManagementSocketTagger(77): setKernelCountSet(10013, 0) failed with errno -2 
05-18 15:16:09.224: W/InputManagerService(77): Starting input on non-focused client [email protected] (uid=10013 pid=187) 
05-18 15:16:09.621: D/dalvikvm(187): GC_FOR_ALLOC freed 2277K, 12% free 18638K/21127K, paused 79ms 
05-18 15:16:34.761: D/dalvikvm(172): GC_CONCURRENT freed 387K, 6% free 9534K/10119K, paused 7ms+4ms 

回答

1

您的主線程無法處理XML解析,所以使用AsyncTask。來完成您的xml解析。

See this Example

+0

thaks亞爾。 。但我不知道在哪裏使用異步任務,因爲即時通訊到Android ..不能清楚地得到你..電話清楚..掃描笏我現在可以做.. –

+0

仔細閱讀本示例... –

+0

它也是lenthy代碼..我可以有簡單的代碼爲相同的PLZ ..它非常難以理解..再次感謝.. –

0

你的錯誤是NetworkOnMainThreadException的解決方案,這意味着你獲取並在主線程解析XML(UI線程),這使得它沒有反應。請參閱Painless Threading以瞭解如何使用AsyncTask在後臺執行長時間任務並在完成時填充UI。

+0

笏我在用戶界面..因爲我是新來的android ..我不能夠清楚地得到你..你可以在電話詳細..從這一點我得到的是..我必須使用AsyncTask ..但我不知道如何以及在哪裏..我應該創建一個新的類?幫助我在這PLZ ..感謝您的早期.. –

0

它報告你應該背景你的網絡功能,因爲在這些類型的任務中阻止Android的UI線程並不是一個好主意。

new FetchXMLAsyncTask.execute(); 

private class FetchXMLAsyncTask extends AsyncTask<Void, Void, Void> { 
     protected void onPreExecute() { 
      } 
      @Override 
      protected Void doInBackground(Void... arg0) { 
       // put your code here where you should fetch xml, parse it and call update ui components. 
       return null; 
      } 

     @Override 
     protected void onPostExecute(final Void unused) { 

     } 
     } 
    } 
+0

我無法得到這個,清楚..可以üPLZ電話詳細..從這1件事我得到的是..我必須使用AsyncTask ..但我不知道如何以及在哪裏..我應該創建一個新的類?幫助我在這plz ..感謝您的早期.. –

+0

如何做.. ..我可以創建內部類r笏我do ..在doInBackground方法我不知道哪些代碼我不得不把.. –

0

看來,你所得到的錯誤是由於服務器上的活動的main()線程執行,以避免這一點,你必須使用的AsyncTask,如下圖所示。

將以下代碼行粘貼到oncreate()或onclicklistener或您放置XMLPARSING CODE的位置。

new DoXMLParsing().execute(); 

下面給出DoXMLParsing是其延伸的AsyncTask執行後臺操作和發佈在UI線程上的結果,而不必操縱線程和/或處理一個內部類。 只需將以下類複製並粘貼到您應該執行XMLPARSING的活動類中。

public class DoXMLParsing extends AsyncTask<Void, Void, Void> { 


protected void onPreExecute() { 
    //any task if you would like to perform before exe the XMLPARSING place the code here 
    } 


    @Override 
    protected Void doInBackground(Void... params) { 

     //cut and paste your whole XMLPARSING code here.. 

     return null; 
     } 

protected void onPostExecute(Void result) { 

    // Here if you wish to do future process for ex. move to another activity do here 

    } 
} 

希望這有助於.. !! :)