對於此給定的代碼,我收到一個異常。我不知道如何清除這一點。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
thaks亞爾。 。但我不知道在哪裏使用異步任務,因爲即時通訊到Android ..不能清楚地得到你..電話清楚..掃描笏我現在可以做.. –
仔細閱讀本示例... –
它也是lenthy代碼..我可以有簡單的代碼爲相同的PLZ ..它非常難以理解..再次感謝.. –