我有一個XML文件正在解析DocumentBuilder
,它解析正確的手機,但平板電腦引發異常。我一直在閱讀,有人建議使用AsyncTask
,我已經實現了,它仍然不解析。有誰知道這個問題或需要做什麼來解決它。以下是我的代碼。蜂窩XML解析錯誤
的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線這是我的座標,它在手機上工作正常,但在平板電腦上獲得null值。
上需要添加或更改了哪些任何建議,可以理解
MainActivity2.java:381,哪條線在381? – AAnkit
@AND_DEV對不起,第381行是Element nameE =(Element)nameL.item(0); nameL = nameE.getChildNodes();代碼 – Dwill
的一部分,所以它似乎nameL在這裏是空的。你有沒有檢查過這個fstElement.getElementsByTagName(「coords」);返回null? – AAnkit