●畫面預覽
=>點擊SAX按鈕後,下方出現Udn RSS新聞列表
●補充說明
(一)RSS的讀取(讀取新聞網站的RSS)
=>RSS是XML格式,android讀進來以後要再用XML的解析工具解開,然後再從中找出我們要的內容
=>RSS一般是要使用RSS閱讀器,指定RSS位置,就可以看到標題和簡短的內容(不能看全文是因為要導引到網站,商業考量)
=>資料交換的格式有:CSV(用,分開)、XML、JSON
=>在JAVA中解XML有兩種方法
1.XML DOM Parser=>xml有結構,作法是把整份文件讀下來再解成整顆樹,是一個樹狀資料結構,再用樹的walkthrough方法,從樹的根開始一條條走,用樹的軸覽方法把資料讀取出來
2.SAX:讀到元件或文字就會跳出相對應的事件出來(我們自己寫的程式),一步一步做
=>一樣要先開起權限<uses-permission android:name="android.permission.INTERNET"/>
(MainActivity)
public void click(View view) {
new Thread(){
@Override
public void run()
{
URL url = null;
try {
url = new URL("https://udn.com/rssfeed/news/2/6638?ch=news";);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
conn.connect();
InputStream inputStream;
inputStream = conn.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(inputStream));
final StringBuilder sb = new StringBuilder();
String str;
while ((str = br.readLine()) != null)
{
sb.append(str);
}
String result = sb.toString();
MyDataHandler dataHandler = new MyDataHandler();
SAXParserFactory spf = SAXParserFactory.newInstance();
SAXParser sp = spf.newSAXParser();
XMLReader xr = sp.getXMLReader();
xr.setContentHandler(dataHandler);
xr.parse(new InputSource(new StringReader(result)));
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (ProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (ParserConfigurationException e) {
e.printStackTrace();
} catch (SAXException e) {
e.printStackTrace();
}
}
}.start();
}
**說明:
1.sax這幾行的作法是固定的,可自己研究內容
2.setContentHandler(dataHandler),設定xml讀取器為我們的class
3. xr.parse(new InputSource(new StringReader(result)));這邊把rss資料載完
(MyDataHandler)=>要自己寫一個class,繼承DefaultHandler(繼承的時候要選org.xml這一個繼承),並override三個method
public class MyDataHandler extends DefaultHandler {
boolean isTitle = false;
boolean isDes=false;
@Override
public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
super.startElement(uri, localName, qName, attributes);
if (qName.equals("title"))
{
isTitle = true;
}
}
@Override
public void endElement(String uri, String localName, String qName) throws SAXException {
super.endElement(uri, localName, qName);
if (qName.equals("title"))
{
isTitle = false;
}
}
@Override
public void characters(char[] ch, int start, int length) throws SAXException {
super.characters(ch, start, length);
if (isTitle)
{
Log.d("NET", new String(ch, start, length));
}
}
}
**說明:
=>每一個item的結構都是item中有title、link、pubDate、descritpion、guid
1.這邊的qName就是tag的名稱(title)
2.最後把整個字元陣列ch取出來轉成字串,同時要取start和length才不會有問題
3.characters():void characters (char[] ch, int start, int length)
=>Receive notification of character data inside an element.
=>By default, do nothing. Application writers may override this method to take specific actions for each chunk of character data
(such as adding the data to a node or buffer, or printing it to a file).
=>Parameters
ch-char: The characters.
start-int: The start position in the character array.
length-int: The number of characters to use from the character array.
(二)RSS xml的取得
=>以udn新聞網為例,選取上方RSS,再到下方選定要Feed的rss類別
=>開啟RSS XML 的URL位址
●程式參考(GitHub):實作讀取新聞網站的RSS
文章標籤
全站熱搜
留言列表