我编写了一些代码来阅读Google新闻中的RSS新闻,并更新ProgressBar
。我的RSS阅读器工作正常,更新已完成,但未调用onPostExecute()
,有人可以解释原因吗?
public class loadNews extends AsyncTask<Void, Integer, Void> {
ArrayList<News> news = new ArrayList<News>();
ArrayList<ArrayList<News>> allData = new ArrayList<ArrayList<News>>();
@Override
protected Void doInBackground(Void... params) {
// TODO Auto-generated method stub
// links to get news
String[] types = { "World", "Bussiness", "Science and Technology",
"Entertainment", "Sport", "Health" };
String[] links = {
// World
"https://news.google.com.eg/news/feeds?pz=1&cf=all&ned=ar_me&hl=ar&topic=w&output=rss",
// Business
"https://news.google.com.eg/news/feeds?pz=1&cf=all&ned=ar_me&hl=ar&topic=b&output=rss",
// science and technology
"https://news.google.com.eg/news/feeds?pz=1&cf=all&ned=ar_me&hl=ar&topic=t&output=rss",
// Entertainment
"https://news.google.com.eg/news/feeds?pz=1&cf=all&ned=ar_me&hl=ar&topic=e&output=rss",
// Sport
"https://news.google.com.eg/news/feeds?pz=1&cf=all&ned=ar_me&hl=ar&topic=s&output=rss",
// Health
"https://news.google.com.eg/news/feeds?pz=1&cf=all&ned=ar_me&hl=ar&topic=m&output=rss" };
URL url = null;
for (int j = 0; j < links.length; j++) {
try {
url = new URL(links[j]);
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
DocumentBuilder builder = null;
try {
builder = DocumentBuilderFactory.newInstance()
.newDocumentBuilder();
} catch (ParserConfigurationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Document doc = null;
try {
doc = builder.parse(url.openStream());
} catch (SAXException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
NodeList nodes = doc.getElementsByTagName("item");
for (int i = 0; i < nodes.getLength(); i++) {
Node node = nodes.item(i);
Element element = (Element) node;
NodeList Ti = element.getElementsByTagName("title");
Element el = (Element) Ti.item(0);
NodeList temp = el.getChildNodes();
String Title = temp.item(0).getNodeValue();
Ti = element.getElementsByTagName("link");
el = (Element) Ti.item(0);
temp = el.getChildNodes();
String Link = temp.item(0).getNodeValue();
// setting news into arraylist
News _news = new News();
_news.title = Title;
Log.i("this is title of news" + i, _news.title);
_news.link = Link;
news.add(_news);
}
allData.add(news);
news.clear();
publishProgress(16);
}
publishProgress(4);
Log.i("end of do in background", "ending");
return null;
}
protected void onProgressUpdate(Integer... progress) {
pb.incrementProgressBy(progress[0]);
}
protected void onPostExcute(String result) {
Log.i("we in postexcute", "onpostexcute");
tv.setText("done");
}
}
答案 0 :(得分:0)
我看到你的代码,在你的帖子执行中没有使用String结果,你只想在某些textview中设置文本,所以如果不需要,那么改变你的代码如下并尝试
public class LoadNews extends AsyncTask<Void, Void, Void> {
//your variables and other methods..,.
@Override
protected Void doInBackground(Void... params) {
//your code
return null;
}
@Override
protected void onPostExecute(Void s)
{
//your code
System.out.println("Just for test, remove it");
}