2012-11-10 4 views
2

Google 뉴스에서 RSS 뉴스를 읽고, ProgressBar을 업데이트하는 코드를 작성합니다. 내 RSS 리더가 올바르게 작동하고 업데이트가 완료되었지만 onPostExecute()이 호출되지 않아 누구나 이유를 설명 할 수 있습니까?AsyncTask.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

나는 코드를 확인하고 게시물에 문자열 결과 아무 소용이없는 실행하고 거기 당신은 그냥 텍스트 뷰의 텍스트를 설정하려면, 그래서 필요하지 않은 경우 다음 다음과 같이 코드를 변경 시도

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"); 
} 
관련 문제