Android-rss-reader
Android-RSSリーダー
RSSはReally Simple Syndicationの略です。 RSSは、Webサイトの更新とコンテンツをユーザーと共有する簡単な方法です。これにより、ユーザーは、更新を行うために毎日サイトにアクセスする必要がなくなります。
RSSの例
RSSは、拡張子が.xmlのWebサイトによって作成されるドキュメントです。 このドキュメントを簡単に解析し、アプリケーションでユーザーに表示できます。 RSSドキュメントは次のようになります。
<rss version="2.0">
<channel>
<title>Sample RSS</title>
<link>http://www.google.com</link>
<description>World's best search engine</description>
</channel>
</rss>
RSS要素
上記のようなRSSドキュメントには、次の要素があります。
Sr.No | 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のファイルを指定します。 ファイルでも、ストリームでもかまいません。 私たちの場合、それはストリームです。その構文は以下のとおりです-
myparser.setInput(stream, null);
最後のステップは、XMLを解析することです。 XMLファイルは、events、Name、Text、AttributesValue e.t.cで構成されます。 したがって、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ファイルをより適切に解析するために、このクラスによって提供される他のメソッドがあります。 これらの方法は以下のとおりです-
Sr.No | Method & description |
---|---|
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クラスの使用を示す例です。 ここで/android/sampleXML.xmlにあるRSSドキュメントを解析して結果を表示できる基本的な解析アプリケーションを作成します。
この例を試すには、実際のデバイスまたはエミュレーターでこれを実行できます。
Steps | Description |
---|---|
1 | You will use Android studio to create an Android application under a package com.example.sairamkrishna.myapplication. |
2 | Modify src/MainActivity.java file to add necessary code. |
3 | Modify the res/layout/activity_main to add respective XML components. |
4 | Create a new java file under src/HandleXML.java to fetch and parse XML data. |
5 | Create a new java file under src/second.java to display result of XML |
5 | Modify AndroidManifest.xml to add necessary internet permission. |
6 | Run the application and choose a running android device and install the application on it and verify the results. |
以下は、変更されたメインアクティビティファイル 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://finddevguides.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/*milliseconds*/);
conn.setConnectTimeout(15000/*milliseconds*/);
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://finddevguides.com/android/sampleXML.xml");
}
}
*res/layout/second_activity.xml* にxmlファイルを作成します
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<WebView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/webView"
android:layout_gravity="center_horizontal"/>
</LinearLayout>
*res/layout/activity_main.xml* の内容を次のように変更します-
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
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">
<TextView android:text="RSS example" android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/textview"
android:textSize="35dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"/>
<TextView
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"/>
<ImageView
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"/>
<EditText
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"/>
<EditText
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"/>
<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"/>
<Button
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"/>
<Button
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"/>
</RelativeLayout>
*res/values/string.xml* を次のように変更します
<resources>
<string name="app_name">My Application</string>
</resources>
これはデフォルトの AndroidManifest.xml です。
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.sairamkrishna.myapplication" >
<uses-permission android:name="android.permission.INTERNET"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
<activity android:name=".second"></activity>
</application>
</manifest>
アプリケーションを実行してみましょう。 環境設定中に AVD を作成したと思います。 Androidスタジオからアプリを実行するには、プロジェクトのアクティビティファイルの1つを開き、ツールバーの[画像を実行:/android/images/eclipse_run.jpg [Eclipse Run Icon]アイコンをクリックします。 AndroidスタジオはAVDにアプリをインストールして起動し、セットアップとアプリケーションで問題がなければ、次のエミュレータウィンドウが表示されます-
Fetch Feedボタンを押すだけでRSSフィードを取得できます。 を押すと、次の画面が表示され、RSSデータが表示されます。
結果ボタンを押すだけで、* http://finddevguides.com/android/sampleXML.xml*にあるXMLが表示されます。