2013-01-07 76 views
0

我正在創建一個從www.zamboangatoday.ph收集數據的android應用程序,獲取所有新聞標題或標題。但我可以檢索只有一個項目可以有人檢查我的代碼。使用jsoup解析來自網站的數據

protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 



      try{ 

       outputTextView = (TextView)findViewById(R.id.textView1); 
       //Document doc = Jsoup.parse(html,"http://www.zamboangatoday.ph"); 
       Document doc = Jsoup.connect("http://www.zamboangatoday.ph/").get(); 
       //Elements tag = doc.select(".even h4 a"); 

       Iterator<Element> iter = doc.select("li.even h4 a").iterator(); 
       //List<Image> images = new ArrayList<Image>(); 
       while(iter.hasNext()) 
       { 
        Element element = iter.next(); 

        outputTextView.setText(element.text()); 

       } 

      }catch(Exception e) 
      { 
       e.printStackTrace(); 
      } 




    } 

回答

1

不舒爾outputTextView是,但這段代碼是什麼成分:

outputTextView.setText(element.text()); 

你設置的文本,覆蓋它與每個迭代。所以你的循環後,將只有最後一個元素文本。如果可能,請使用類似outputTextView.append(element.text());的東西 - 否則使用StringBuilder並設置循環後面的文本。


outputTextView = (TextView)findViewById(R.id.textView1); 
//Document doc = Jsoup.parse(html,"http://www.zamboangatoday.ph"); 
Document doc = Jsoup.connect("http://www.zamboangatoday.ph/").get(); 
//Elements tag = doc.select(".even h4 a"); 

Iterator<Element> iter = doc.select("li.even h4 a").iterator(); 
//List<Image> images = new ArrayList<Image>(); 
while(iter.hasNext()) 
{ 
    Element element = iter.next(); 

    /* 
    * Append the text - don't overwrite it 
    * Node: Maybe you have to add a new line ('\n'). 
    */ 
    outputTextView.append(element.text()); 

} 
+0

感謝兄弟。它工作 – user1643284

+0

好吧,我完成了。有人可以幫助我如何使用列表視圖來實現它。 – user1643284

+0

如果這解決了你的問題,你可以[接受](http://stackoverflow.com/faq#howtoask)這個答案。 – ollo