Вопрос

I have a XML file that is being parsed by DocumentBuilder, it parses correctly on the phones but the tablet throws an exception. I've been reading and it was suggested to use a AsyncTask, I've implemented that and it is still not parsing. Does anyone know of this issue or what needs to be done to fix it. Below is my code.

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;
        }

This is where I am calling it at:

       @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();
           }
    }

This the format of my 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>                 

This is my stack trace results:

  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)

Looks to be happening at line 381 which is my coords, it works fine on the phones but gets the null value on the tablets.

Any suggestions on what needs to be added or changed will be appreciated

Это было полезно?

Решение

I found the solution to my problem. I basically had to redo my loop that stored the data. Don't know why the original way worked for the phones and not the tablet.

Другие советы

Node node = ProfileNode.item(gd);

NodeList nodeList = node.getChildNodes();

String name = ((Node) (nodeList).item(0)).getNodeValue();

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top