您好我正在使用将解析值并使用列表视图显示的 android 应用程序。我正在使用 XML 解析方法来解析数据并成功显示在列表视图中。现在的问题是当我尝试解析没有值(NULL)的xml标记时,它会出错并强制关闭应用程序。我尝试如果节点具有某些值然后只允许解析它仍然显示一些问题并且只有第一个标签显示在第一个列表项中。其他列表项没有显示。
Below is the xml tags to parse.
<UserScore>
<GameId>daily_1349375400</GameId>
<Location></Location>
<GameName>Football - 5th October 2013</GameName>
<Date>Oct 5, 2013</Date>
</UserScore>
−
<UserScore>
<GameId>136</GameId>
<GameName>Company Cricket</GameName>
<Location>France </Location>
<Date>Jan 6, 2012</Date>
</UserScore>
For the first time the location tag having "france" as its value, but when again played the second location tag contains nothing.
Here is my coding please tell me the solution.
public class AsynTaskScoreboard extends AsyncTask<Void, Void, Void> {
private ProgressDialog dialog = ProgressDialog.show(getParent(), "Retrieving data ", " Loading. Please wait ... ", true);
// can use UI thread here
protected void onPreExecute() {
this.dialog.setMessage("Loading...");
this.dialog.setCancelable(false);
this.dialog.show();
}
@Override
protected Void doInBackground(Void... params) {
try {
URL xpto = new URL(my url here);
HttpURLConnection conn;
conn = (HttpURLConnection) xpto.openConnection();
conn.setDoInput(true);
conn.connect();
InputStream is = conn.getInputStream();
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
try {
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc = db.parse(is);
doc.getDocumentElement().normalize();
NodeList nodeList;
nodeList = doc.getElementsByTagName("TotalPoints");
Log.d("Background", "TotalPoints " + nodeList);
for (int i = 0; i < nodeList.getLength(); i++) {
Node name = nodeList.item(i);
NodeList nodeEle = name.getChildNodes();
strTotalPoints = ((Node) nodeEle.item(0)).getNodeValue();
Log.d("Background", "TotalPoints " + nodeList);
}
nodeList = doc.getElementsByTagName("ChallengesPlayed");
for (int i = 0; i < nodeList.getLength(); i++) {
Node name = nodeList.item(i);
NodeList nodeEle = name.getChildNodes();
strChallengesPoint = ((Node) nodeEle.item(0)).getNodeValue();
}
android.os.Message msg = Message.obtain();
msg.what = 11;
handler.sendMessage(msg);
/**
* Check whether it is my points or high score
*/
switch (flagMypointsOrHighScore) {
case 0:
nodeList = doc.getElementsByTagName("UserScore");
// Define Array based on nodelist
arrLength = nodeList.getLength();
strGameid = new String[arrLength];
strGameNameArr = new String[arrLength];
strGameLocationArr = new String[arrLength];
strGameDate = new String[arrLength];
String[] strXMLTag = { "GameId", "GameName", "Location", "Date" };
for (int i = 0; i < arrLength; i++) {
Node node = nodeList.item(i);
Log.d("scoreList", "node " + node);
Element fstElmnt = (Element) node;
Log.d("scoreList", "fstElmnt " + fstElmnt);
for (int j = 0; j < strXMLTag.length; j++) {
NodeList scoreList = fstElmnt.getElementsByTagName(strXMLTag[j]);
Element scoreElement = (Element) scoreList.item(0);
scoreList = scoreElement.getChildNodes();
switch (j) {
case 0:
strGameid[i] = ((Node) scoreList.item(0)).getNodeValue();
break;
case 1:
strGameNameArr[i] = ((Node) scoreList.item(0)).getNodeValue();
break;
case 2:
strGameLocationArr[i]=((Node) scoreList.item(0)).getNodeValue();
break;
case 3:
strGameDate[i] = ((Node) scoreList.item(0)).getNodeValue();
break;
}
}
}// End of for loop
// Your rank
break;
case 1:// Top rank 5
nodeList = doc.getElementsByTagName("Top5Rank");
// Define Array based on nodelist
arrLength = nodeList.getLength();
strGameNameArr = new String[arrLength];
strGameLocationArr = new String[arrLength];
strGameDate = new String[arrLength];
for (int i = 0; i < arrLength; i++) {
Node node = nodeList.item(i);
Element fstElmnt = (Element) node;
NodeList rankList = fstElmnt.getElementsByTagName("Person");
//
int rankListLength = rankList.getLength();
if (strPersonNameArr == null) {
strPersonNameArr = new String[rankListLength];
strPersonRankArr = new String[rankListLength];
strPersoonPointsArr = new String[rankListLength];
}
for (int j = 0; j < rankListLength; j++) {
rankList = fstElmnt.getElementsByTagName("Person");
Element rankElement = (Element) rankList.item(j);
rankList = rankElement.getChildNodes();
strPersonNameArr[j] = rankElement.getAttribute("Name");
strPersonRankArr[j] = rankElement.getAttribute("Rank");
strPersoonPointsArr[j] = rankElement.getAttribute("Points");
}
}// End of for loop
break;
}// End of switch
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}// try below db 2try
} catch (Exception e) {
// TODO Auto-generated catch block
if (e.toString().equalsIgnoreCase("java.net.SocketException: Network unreachable")) {
android.os.Message msg = android.os.Message.obtain();
msg.what = 0;
handler.sendMessage(msg);
}
e.printStackTrace();
}
return null;
}// End of do in background method
// do the stuff after the heavy task
protected void onPostExecute(Void result) {
android.os.Message msg = Message.obtain();
switch (flagMypointsOrHighScore) {
case 0:
listViewMyPoints.setAdapter(new JR_LB_ListAdapter(ScoreboardActivity.this));
break;
case 1:
listViewHighScores.setAdapter(new JR_LB_ListAdapter_HighScores(ScoreboardActivity.this));
// Set textview value in the
msg.what = 10;
handler.sendMessage(msg);
break;
default:
}
if (this.dialog.isShowing()) {
this.dialog.dismiss();
}
}// End of on post execute method
@Override
protected void onProgressUpdate(Void... values) {
// TODO Auto-generated method stub
Totalpoint.setText(strTotalPoints);
challengeplay.setText(strChallengesPoint);
txtViewRank.setText(strRank);
txtViewPoint.setText(strPoint);
txtViewPersonName.setText(strPersonName);
Log.d("ProgressUpdate", "strTotalPoints " + strTotalPoints);
Log.d("ProgressUpdate", "strChallengesPoint " + strChallengesPoint);
Log.d("ProgressUpdate", "strRank " + strRank);
Log.d("ProgressUpdate", "strPoint " + strPoint);
}
}// End of Asynchronous task class
在异步任务之后,我通过意图将值传递给下一个活动。在那个值也传递为空。使用该值,我将进行进一步的活动。请告诉如何克服这个问题。