2

我编写了一些代码来从 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");

        }
    }
4

1 回答 1

0

我看到了您的代码,并且在您的帖子执行中没有使用字符串结果,您只想在某些文本视图中设置文本,因此如果不需要,请按以下方式更改您的代码并尝试

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");
}
于 2012-11-10T14:53:15.027 回答