我有onPostExecute
的{{1}}代码。它运行良好。但是,当我删除注释的代码时,我从未收到AsyncTask
事件(No Toast)。
onPostExecute
@Override
protected void onPostExecute(String result) {
internetData = result;
tempview.append(result + "\n");
Toast.makeText(getApplicationContext(), internetData.toString(), 1).show();
//setServiceId();
}
事件和后续事件(有意删除网址),setServiceId
是我的GetXMLData
:
AsyncTask
上面的最后一个事件public void setServiceId() {
if (internetData != null) {
try {
XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
factory.setNamespaceAware(true);
XmlPullParser xpp = factory.newPullParser();
xpp.setInput(new StringReader(internetData));
int eventType = xpp.getEventType();
while (eventType != XmlPullParser.END_DOCUMENT) {
if (eventType == XmlPullParser.TEXT) {
serviceId = xpp.getText();
serviceHistory = "www.google.com/" + serviceId + "/history";
serviceUsage = "www.google.com/" + serviceId + "/usage";
serviceDetails = "www.google.com/" + serviceId + "/service";
Toast.makeText(getApplicationContext(), serviceId, 1).show();
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
updateHistory();
updateDetails();
updateUsage();
}
public void updateHistory() { //Never called
new GetXMLData().execute(serviceHistory);
Toast.makeText(getApplicationContext(), "Update History", 1).show();
}
public void updateUsage() { //Never called
new GetXMLData().execute(serviceUsage);
Toast.makeText(getApplicationContext(), "Update Usage", 1).show();
}
public void updateDetails() { //Never called
new GetXMLData().execute(serviceDetails);
Toast.makeText(getApplicationContext(), "Update Details", 1).show();
}
public void updateData() { //First call
new GetXMLData().execute(serviceListing);
Toast.makeText(getApplicationContext(), "Update Data", 1).show();
}
被调用,而updateData
事件未被注释掉时Toast
似乎永远不会消失。除此之外,不会检索XML数据。
当它被注释掉时,我会收到setServiceId
事件的XML数据,但没有以下事件,我也没有从updateData()
获得Toast
。有关为何发生这种情况的任何建议吗?
答案 0 :(得分:1)
你的while循环条件实际上有一个错误,它将循环无限并使你的应用程序挂起:
while (eventType != XmlPullParser.END_DOCUMENT) {
if (eventType == XmlPullParser.TEXT) {
serviceId = xpp.getText();
serviceHistory = "www.google.com/" + serviceId + "/history";
serviceUsage = "www.google.com/" + serviceId + "/usage";
serviceDetails = "www.google.com/" + serviceId + "/service";
Toast.makeText(getApplicationContext(), serviceId, 1).show();
}
}
在循环中添加下一行:
eventType = xpp.next();