2012-06-04 85 views
0

我正在製作一個Android應用程序,我需要從這個頁面讀取最新消息(和標籤上寫着「NYHEDER」: http://www.stormthebuildingfest.com/ 然後,當然,打印出來。在屏幕上Android - 從網站閱讀新聞

我已經看到了不同的地方,也許JSOUP可以幫助解決這個問題 - 但JSOUP食譜似乎並沒有幫助我

它甚至有可能從該網頁獲取新聞。 ?如何? 或者如果是JSOUP - 任何人都可以引用我該食譜的哪一部分適合這個問題? http://jsoup.org/cookbook/

在此先感謝。

+0

您可以獲取新聞的RSS或XML提要嗎?這會讓事情變得更容易。 – dotty

+0

我對此有點新,但是我想從新聞網站獲得這個可能嗎?因爲我願意嘗試,如果是的話。任何提示? – Rad

回答

0

基本上,你需要做的是這樣的:

//This line will get you the whole page 
Document doc = Jsoup.connect("http://www.url.com/section.script").get(); 

//This other line will make sure you get the specific info you're looking for 
Elements elems = doc.select("some element"); 


//Or if you wanna just print it all out, you can simply 
System.out.println(doc); 

//You can also print just the text (Without ANY tags, or charset issues) 
System.out.println(doc.text()) 

//The .text(); method works for you the variable Elements as 
//well. If you got some info, and just want its text, that's 
//the best way to go 

此外,我會研究Jsoup selector API。

+0

嗯,我試過了。但是當我調試 'Document doc =(Document)Jsoup.connect(「www.stormthebuildingfest.com」);'我得到「Source not found」錯誤。它不會繼續下一行? – Rad

+0

那麼,你是否省略了「http://」和.get();?下面一行對我來說工作得很好:(注意:這裏有一個空格,只是它不能被識別爲鏈接) Document doc = Jsoup.connect(「http:// www.stormthebuildingfest.com」)。得到(); –

+0

不知何故複製你的代碼工作。非常感謝! 下面的工作代碼: org.jsoup.nodes.Document doc; \t \t \t \t \t doc = Jsoup.connect(「http:// www.stormthebuildingfest.com」)。get(); – Rad

0

你可以使用函數XML pull parser - 爲了這個工作必須有一個XML/RSS。

http://android-developers.blogspot.co.uk/2011/12/watch-out-for-xmlpullparsernexttext.html

的理論好文章。

+0

對不起,如果我看起來很愚蠢。但是,即使鏈接不是XML,我可以將它作爲XML嗎? 要檢查鏈接! – Rad

+0

不,您必須解析標籤內的XML。 http://gta5news.com/feed我有'標題'標籤,並在其中的數據和評論。你需要解析這個XML並根據你需要的信息進行排序並填充一個列表視圖。 – TheBlueCat

+0

嗯。但是,您的鏈接gta5news.com/feed爲我提供了一個實際的XML文件,這顯然更容易處理。任何方式我可以使我的鏈接stormthebuildingfest.com成爲一個XML?如果不是,我怎麼知道「隱藏」標籤?因爲如果它是一個XML文件就像你的鏈接,我知道如何解析它。 – Rad