天天看點

android rss 源碼,Android RSS閱讀器

RSS代表Really SimpleSyndication。RSS是一種與使用者分享網站更新和内容的簡便方法,是以使用者可能無需每天通路您的網站進行任何類型的更新。

RSS示例

RSS是由擴充名為.xml的網站建立的文檔。您可以輕松解析此文檔并将其顯示給應用程式中的使用者。RSS文檔看起來像這樣。

Sample RSS

http://www.google.com

World's best search engine

RSS元素

如上所述的RSS文檔具有以下元素。

序号

Component & description

1

channel

此元素用于描述RSS提要

2

title

定義頻道的标題

3

link

定義通道的超連結

4

description

描述頻道

解析RSS

解析RSS文檔更像是解析XML。現在讓我們看看如何解析XML文檔。

為此,我們将建立XMLPullParser對象,但為了建立它,我們将首先建立XmlPullParserFactory對象,然後調用其newPullParser()方法來建立XMLPullParser。其文法如下

private XmlPullParserFactory xmlFactoryObject = XmlPullParserFactory.newInstance();

private XmlPullParser myparser = xmlFactoryObject.newPullParser();

下一步涉及為包含XML的XmlPullParser指定檔案。它可以是檔案,也可以是Stream。在我們的例子中,它是一個流。它的文法如下 -

myparser.setInput(stream, null);

最後一步是解析XML。XML檔案由事件,Name,Text,AttributesValue等組成。是以,XMLPullParser具有單獨的函數,用于解析XML檔案的每個元件。其文法如下

int event = myParser.getEventType();

while (event != XmlPullParser.END_DOCUMENT) {

String name=myParser.getName();

switch (event){

case XmlPullParser.START_TAG:

break;

case XmlPullParser.END_TAG:

if(name.equals("temperature")){

temperature = myParser.getAttributeValue(null,"value");

}

break;

}

event = myParser.next();

}

方法 getEventType 傳回發生的事件類型。例如:文檔開始,标記開始等方法 getName

傳回标記的名稱,因為我們隻對溫度感興趣,是以我們隻檢查條件語句,如果我們得到溫度标記,我們調用方法 getAttributeValue

傳回給我們溫度标簽的價值。

除了這些方法之外,此類還提供了其他方法來更好地解析XML檔案。這些方法如下 -

序号

方法和描述

1

getAttributeCount()

此方法僅傳回目前開始标記的屬性數。

2

getAttributeName(int index)

此方法傳回索引值指定的屬性的名稱。

3

getColumnNumber()

此方法傳回傳回目前列号,從0開始。

4

getDepth()

此方法傳回傳回元素的目前深度。

5

getLineNumber()

傳回目前行号,從1開始。

6

getNamespace()

此方法傳回目前元素的名稱空間URI。

7

getPrefix()

此方法傳回目前元素的字首。

8

getName()

此方法傳回标記的名稱。

9

getText()

此方法傳回該特定元素的文本。

10

isWhitespace()

此方法檢查目前TEXT事件是否僅包含空格字元。

這是一個示範XMLPullParser類使用的示例。它建立了一個基本的Parsing應用程式,允許您在/android/sampleXML.xml中解析此處存在的RSS文檔,然後顯示結果。

要試驗此示例,您可以在實際裝置或模拟器中運作此示例。

序号

描述

1

您将使用Android studio在com.example.sairamkrishna.myapplication包下建立Android應用程式。

2

修改src / MainActivity.java檔案以添加必要的代碼。

3

修改res / layout / activity_main以添加相應的XML元件。

4

在src / HandleXML.java下建立一個新的java檔案來擷取和解析XML資料。

5

在src / second.java下建立一個新的java檔案以顯示XML的結果

5

修改AndroidManifest.xml以添加必要的Internet權限。

6

運作應用程式并選擇正在運作的Android裝置并在其上安裝應用程式并驗證結果。

以下是修改後的主活動檔案 src / MainActivity.java 的内容 。

package com.example.sairamkrishna.myapplication;

import android.app.Activity;

import android.content.Intent;

import android.os.Bundle;

import android.view.View;

import android.widget.Button;

import android.widget.EditText;

public class MainActivity extends Activity {

EditText title,link,description;

Button b1,b2;

private String finalUrl="http://codingdict.com/android/sampleXML.xml";

private HandleXML obj;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

title = (EditText) findViewById(R.id.editText);

link = (EditText) findViewById(R.id.editText2);

description = (EditText) findViewById(R.id.editText3);

b1=(Button)findViewById(R.id.button);

b2=(Button)findViewById(R.id.button2);

b1.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View v) {

obj = new HandleXML(finalUrl);

obj.fetchXML();

while(obj.parsingComplete);

title.setText(obj.getTitle());

link.setText(obj.getLink());

description.setText(obj.getDescription());

}

});

b2.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View v) {

Intent in=new Intent(MainActivity.this,second.class);

startActivity(in);

}

});

}

}

以下是java檔案 src / HandleXML.java 的内容 。

package com.example.rssreader;

import java.io.InputStream;

import java.net.HttpURLConnection;

import java.net.URL;

import org.xmlpull.v1.XmlPullParser;

import org.xmlpull.v1.XmlPullParserFactory;

import android.util.Log;

public class HandleXML {

private String title = "title";

private String link = "link";

private String description = "description";

private String urlString = null;

private XmlPullParserFactory xmlFactoryObject;

public volatile boolean parsingComplete = true;

public HandleXML(String url){

this.urlString = url;

}

public String getTitle(){

return title;

}

public String getLink(){

return link;

}

public String getDescription(){

return description;

}

public void parseXMLAndStoreIt(XmlPullParser myParser) {

int event;

String text=null;

try {

event = myParser.getEventType();

while (event != XmlPullParser.END_DOCUMENT) {

String name=myParser.getName();

switch (event){

case XmlPullParser.START_TAG:

break;

case XmlPullParser.TEXT:

text = myParser.getText();

break;

case XmlPullParser.END_TAG:

if(name.equals("title")){

title = text;

}

else if(name.equals("link")){

link = text;

}

else if(name.equals("description")){

description = text;

}

else{

}

break;

}

event = myParser.next();

}

parsingComplete = false;

}

catch (Exception e) {

e.printStackTrace();

}

}

public void fetchXML(){

Thread thread = new Thread(new Runnable(){

@Override

public void run() {

try {

URL url = new URL(urlString);

HttpURLConnection conn = (HttpURLConnection) url.openConnection();

conn.setReadTimeout(10000 );

conn.setConnectTimeout(15000 );

conn.setRequestMethod("GET");

conn.setDoInput(true);

// Starts the query

conn.connect();

InputStream stream = conn.getInputStream();

xmlFactoryObject = XmlPullParserFactory.newInstance();

XmlPullParser myparser = xmlFactoryObject.newPullParser();

myparser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, false);

myparser.setInput(stream, null);

parseXMLAndStoreIt(myparser);

stream.close();

}

catch (Exception e) {

}

}

});

thread.start();

}

}

建立一個檔案,并在目錄 java / second.java 下命名為second.java檔案

package com. example.sairamkrishna.myapplication;

import android.app.Activity;

import android.os.Bundle;

import android.webkit.WebView;

public class second extends Activity {

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.second_activity);

WebView w1=(WebView)findViewById(R.id.webView);

w1.loadUrl("http://codingdict.com/android/sampleXML.xml");

}

}

在 res / layout / second_main.xml 建立一個xml檔案

xml version="1.0" encoding="utf-8"?>

android:orientation="vertical" android:layout_width="match_parent"

android:layout_height="match_parent">

android:layout_width="match_parent"

android:layout_height="match_parent"

android:id="@+id/webView"

android:layout_gravity="center_horizontal" />

将 res / layout / activity_main.xml 的内容修改為以下内容

xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"

android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"

android:paddingRight="@dimen/activity_horizontal_margin"

android:paddingTop="@dimen/activity_vertical_margin"

android:paddingBottom="@dimen/activity_vertical_margin"

tools:context=".MainActivity"

android:transitionGroup="true">

android:layout_height="wrap_content"

android:id="@+id/textview"

android:textSize="35dp"

android:layout_alignParentTop="true"

android:layout_centerHorizontal="true" />

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="Tutorials point"

android:id="@+id/textView"

android:layout_below="@+id/textview"

android:layout_centerHorizontal="true"

android:textColor="#ff7aff24"

android:textSize="35dp" />

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:id="@+id/imageView"

android:src="@drawable/abc"

android:layout_below="@+id/textView"

android:layout_centerHorizontal="true"

android:theme="@style/Base.TextAppearance.AppCompat" />

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:id="@+id/editText"

android:layout_below="@+id/imageView"

android:hint="Tittle"

android:textColorHint="#ff69ff0e"

android:layout_alignParentRight="true"

android:layout_alignParentEnd="true"

android:layout_alignParentLeft="true"

android:layout_alignParentStart="true" />

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:id="@+id/editText2"

android:layout_below="@+id/editText"

android:layout_alignLeft="@+id/editText"

android:layout_alignStart="@+id/editText"

android:textColorHint="#ff21ff11"

android:hint="Link"

android:layout_alignRight="@+id/editText"

android:layout_alignEnd="@+id/editText" />

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:id="@+id/editText3"

android:layout_below="@+id/editText2"

android:layout_alignLeft="@+id/editText2"

android:layout_alignStart="@+id/editText2"

android:hint="Description"

android:textColorHint="#ff33ff20"

android:layout_alignRight="@+id/editText2"

android:layout_alignEnd="@+id/editText2" />

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="Fetch"

android:id="@+id/button"

android:layout_below="@+id/editText3"

android:layout_alignParentLeft="true"

android:layout_alignParentStart="true"

android:layout_toLeftOf="@+id/imageView"

android:layout_toStartOf="@+id/imageView" />

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="Result"

android:id="@+id/button2"

android:layout_alignTop="@+id/button"

android:layout_alignRight="@+id/editText3"

android:layout_alignEnd="@+id/editText3" />

将 res / values / string.xml 修改為以下内容

My Application

這是預設的 AndroidManifest.xml 。

package="com.example.sairamkrishna.myapplication" >

android:allowBackup="true"

android:icon="@mipmap/ic_launcher"

android:label="@string/app_name"

android:theme="@style/AppTheme" >

android:name=".MainActivity"

android:label="@string/app_name" >

讓我們嘗試運作您的應用程式。我假設您在進行環境設定時建立了 AVD。要從Android工作室運作應用程式,請打開項目的某個活動檔案,然後單擊

android rss 源碼,Android RSS閱讀器

工具欄中的“運作” 圖示。Android工作室在您的AVD上安裝應用程式并啟動它,如果您的設定和應用程式一切正常,它将顯示以下模拟器視窗

android rss 源碼,Android RSS閱讀器

隻需按Fetch Feed按鈕即可擷取RSS feed。按下後,将出現以下螢幕,顯示RSS資料。

android rss 源碼,Android RSS閱讀器
android rss 源碼,Android RSS閱讀器