我正在解析值数据并使用 xml 解析显示的项目中工作。
它在最高 2.3 的 android 版本中运行良好,但在最高 4.0 时无法正常运行。
我发现,在异步任务中,onPreExeucte 方法运行良好,但是当进程到达 doInBackground 方法时它将无法正常工作。并显示空异常行号 251。
这是我的代码,帮我解决这个问题。
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(strMainUrl + "score.php?UserId=" + EventListActivity.UserID);
Log.e("Total urlScore board", "scr bord full url " + xpto);
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");
Node node1 = nodeList.item(0);
Element fstElmnt1 = (Element) node1;
NodeList scoreList1 = fstElmnt1.getElementsByTagName("TotalPoints");
Element scoreElement1 = (Element) scoreList1.item(0);
scoreList1 = scoreElement1.getChildNodes();
strTotalPoints = ((Node) scoreList1.item(0)).getNodeValue();
Log.d("SetText", "strTotalPoints " + strTotalPoints);
nodeList = doc.getElementsByTagName("ChallengesPlayed");
node1 = nodeList.item(0);
fstElmnt1 = (Element) node1;
scoreList1 = fstElmnt1.getElementsByTagName("ChallengesPlayed");
scoreElement1 = (Element) scoreList1.item(0);
scoreList1 = scoreElement1.getChildNodes();
strChallengesPoint = ((Node) scoreList1.item(0)).getNodeValue();
Log.d("SetText", "strChallengesPoint " + strChallengesPoint);
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];
Log.d("SetText", "arrLength " + arrLength);
Log.d("SetText", "strGameid " + strGameid);
Log.d("SetText", "strGameNameArr " + strGameNameArr);
Log.d("SetText", "strGameLocationArr " + strGameLocationArr);
Log.d("SetText", "strGameDate " + strGameDate);
String[] strXMLTag = { "GameId", "GameName", "Location", "Date" };
for (int i = 0; i < arrLength; i++) {
Node node = nodeList.item(i);
Element fstElmnt = (Element) node;
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();
// location =
// strGameLocationArr[i].toString().trim();
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
// // Your rank
//
// nodeList = doc.getElementsByTagName("YourRank");
//
// // Define Array based on nodelist
//
// for (int i = 0; i < arrLength; i++) {
// //
// Node node = nodeList.item(i);
// Element fstElmnt = (Element) node;
//
// NodeList rankList = fstElmnt
// .getElementsByTagName("Person");
// Element rankElement = (Element) rankList.item(0);
// rankList = rankElement.getChildNodes();
// strPersonName = rankElement.getAttribute("Name");
// strRank = rankElement.getAttribute("Rank");
// strPoint = 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);
}
}// End of Asynchronous task class