-1

对于这个给定的代码,我遇到了一个异常。我不知道如何清除这个。

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 com.android.internal.view.IInputMethodClient$Stub$Proxy@41558698 (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
4

4 回答 4

1

您的主线程无法处理 XML 解析。所以使用AsyncTask来完成您的 xml 解析。

请参阅此示例

于 2012-05-18T09:59:50.547 回答
0

您的错误NetworkOnMainThreadException意味着您正在主线程(UI 线程)上获取和解析 XML,这使其无响应。请参阅无痛线程以了解如何使用 anAsyncTask在后台执行长时间任务并在完成时填充您的 UI。

于 2012-05-18T09:57:16.787 回答
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) {

         }   
      }
  }
于 2012-05-18T09:57:32.677 回答
0

似乎您收到的错误是由于在 main() 线程上执行的 SERVER 活动,以避免这种情况,您必须使用 AsyncTask,如下所示。

将下面这行代码粘贴到您的 oncreate() 或 onclicklistener 或您放置 XMLPARSING 代码的任何位置。

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

     }
 }

希望这可以帮助..!!:)

于 2013-03-18T21:12:09.767 回答