1

我有一个正在解析的 XML 文件DocumentBuilder,它可以在手机上正确解析,但平板电脑会引发异常。我一直在阅读,有人建议使用 a AsyncTask,我已经实现了它,但它仍然没有解析。有谁知道这个问题或需要做些什么来解决它。下面是我的代码。

异步任务

@Override
protected void onCreate(Bundle savedInstanceState) {
DownloadWebPageTask task = new DownloadWebPageTask();
task.execute(new String[] { "url" });
}
 private class DownloadWebPageTask extends AsyncTask<String, Void, String> {
    @Override
    protected String doInBackground(String... urls) {
        //String response = "";
        for (String url : urls) {
            HttpClient httpclient = new DefaultHttpClient();
            HttpGet httpget = new HttpGet(url);

            HttpResponse responseGet = null;
            try {
                responseGet = httpclient.execute(httpget);
                final int status = responseGet.getStatusLine().getStatusCode();
                if (status != HttpStatus.SC_OK) {
                    // give message
                //  Log.i("Error", "No Connection");
                }
            } catch (ClientProtocolException e) {
                // TODO Auto-generated catch block
                  e.printStackTrace();
                  //Log.i("caught error","ClientException");
            } catch (IOException e) {
                // TODO Auto-generated catch block
                  e.printStackTrace();
                //  Log.i("caught error","IOException");
            }
            //String responseBody = null;
            try {
                responseBody = EntityUtils.toString(responseGet.getEntity());
            } catch (ClientProtocolException e) {
                // TODO Auto-generated catch block
                  e.printStackTrace();
                //  Log.i("caught error","ClientException");
            } catch (IOException e) {
                // TODO Auto-generated catch block
                  e.printStackTrace();
                //  Log.i("caught error","IOException");
            }
        }

        return responseBody;
        }

这就是我所说的:

       @Override
    protected void onPostExecute(String result) {
    //  Log.i("returndata",result);


          ArrayList<String> custCount = new ArrayList<String>();
          ArrayList<String> custMinMax = new ArrayList<String>();
          ArrayList<String> hexcode = new ArrayList<String>();
          List<HashMap<String, String>> colorData = new LinkedList<HashMap<String, String>>();


          try {
              DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
              DocumentBuilder db = dbf.newDocumentBuilder();
             //encode the xml to UTF -8
              ByteArrayInputStream encXML = new ByteArrayInputStream(result.getBytes("UTF8"));

              Document doc = db.parse(encXML);
              doc.getDocumentElement().normalize();
              Log.i("XML parsing OK","ok");
       try { 
              NodeList nodeList = doc.getElementsByTagName("Section");
              NodeList nameList = null;
              for (int cc = 0; cc < nodeList.getLength(); cc++) {

                  Node custcountNode = nodeList.item(cc);

                  Element custcountElement = (Element) custcountNode;
                  NodeList custcountList = custcountElement
                          .getElementsByTagName("custcount");
                  Element custcountnameE = (Element) custcountList.item(0);
                  custcountList = custcountnameE.getChildNodes();

                  custcount = ((Node) custcountList.item(0))
                          .getNodeValue();
                  custCount.add(new String(custcount));

              }

              for (int i = 0; i < nodeList.getLength(); i++) {
                  rectOptions = new PolygonOptions();
                  Node node = nodeList.item(i);

                  Element locElement = (Element) node;
                  nameList = locElement
                          .getElementsByTagName("coords");

                  int curCustCount = Integer.valueOf(custCount.get(i));

                  for (int z = 0; z < colorData.size(); z++) {
                      int custmin = Integer.valueOf(colorData.get(z).get(
                              "custmin"));
                      int custmax = Integer.valueOf(colorData.get(z).get(
                              "custmax"));

                      if (curCustCount >= custmin && curCustCount <= custmax) {
                          curColor = (colorData.get(z).get("hexcode"));

                          break;
                      }

                  }

                  points = new ArrayList<String[]>();


                  for (int j = 0; j < nameList.getLength(); j++) {

                      Node nodel = nameList.item(j);

                      Element fstElement = (Element) nodel;

                      NodeList nameL = fstElement.getElementsByTagName("coords");
                      Element nameE = (Element) nameL.item(0);


                      nameL = nameE.getChildNodes();

                      String latit = ((Node) nameL.item(0)).getNodeValue();



                      String[] latt = latit.split(",");

                      points.add(latt);
                 }
                     // Do something here with points

          }
                    } 
                      catch (Exception e){
                      e.printStackTrace();

                 }
          }  
                   catch (Exception e) {
                   e.printStackTrace();
           }
    }

这是我的格式XML

   <Section>
    <custcount>3</custcount>
    <location>
       <coords>35.25010,-90.08342</coords>
       <coords>35.29177,-90.08342</coords>
       <coords>35.29177,-90.04175</coords>
       <coords>35.25010,-90.04175</coords>
       <coords>35.25010,-90.08342</coords>
   </location>
 </Section>
<Section>
    <custcount>3</custcount>
    <location>
       <coords>35.25040,-90.08342</coords>
       <coords>35.29477,-90.08342</coords>
       <coords>35.29477,-90.04173</coords>
       <coords>35.25010,-90.04175</coords>
       <coords>35.25010,-90.08342</coords>
   </location>
 </Section>                 

这是我的堆栈跟踪结果:

  01-31 09:50:07.541: W/System.err(21462): java.lang.NullPointerException
  01-31 09:50:07.541: W/System.err(21462):  at com.mlgw.map.MainActivity2$DownloadWebPageTask.onPostExecute(MainActivity2.java:381)
  01-31 09:50:07.541: W/System.err(21462):  at com.mlgw.map.MainActivity2$DownloadWebPageTask.onPostExecute(MainActivity2.java:1)
  01-31 09:50:07.541: W/System.err(21462):  at android.os.AsyncTask.finish(AsyncTask.java:590)
  01-31 09:50:07.541: W/System.err(21462):  at android.os.AsyncTask.access$600(AsyncTask.java:149)
  01-31 09:50:07.541: W/System.err(21462):  at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:603)
  01-31 09:50:07.541: W/System.err(21462):  at android.os.Handler.dispatchMessage(Handler.java:99)
  01-31 09:50:07.541: W/System.err(21462):  at android.os.Looper.loop(Looper.java:132)
  01-31 09:50:07.541: W/System.err(21462):  at android.app.ActivityThread.main(ActivityThread.java:4129)
  01-31 09:50:07.551: W/System.err(21462):  at java.lang.reflect.Method.invokeNative(Native Method)
  01-31 09:50:07.551: W/System.err(21462):  at java.lang.reflect.Method.invoke(Method.java:491)
  01-31 09:50:07.551: W/System.err(21462):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:855)
  01-31 09:50:07.551: W/System.err(21462):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:613)
  01-31 09:50:07.551: W/System.err(21462):  at dalvik.system.NativeStart.main(Native Method)

看起来发生在第 381 行,这是我的坐标,它在手机上运行良好,但在平板电脑上获得空值。

任何需要添加或更改的建议将不胜感激

4

2 回答 2

0

我找到了解决我的问题的方法。我基本上不得不重做存储数据的循环。不知道为什么原来的方式适用于手机而不是平板电脑。

于 2013-01-31T20:30:50.910 回答
0

节点节点 = ProfileNode.item(gd);

NodeList nodeList = node.getChildNodes();

字符串名称 = ((Node) (nodeList).item(0)).getNodeValue();

于 2013-05-30T12:10:42.097 回答