با عرض سلام و وقت بخیر خدمت کاربران سایت پی وی لرن . و کاربرانی که دوره کامل آموزش برنامه نویسی اندروید را دنبال می کنند. RSS مخفف عبارت Really Simple Syndication است. RSS Reader در اندروید یک راه آسان برای به اشتراک گذاری آپدیت ها و محتوای وب سایت خود از طریق اپلیکیشن با کاربرانی که از این اپلیکیشن استفاده می کنند است. با استفاده از قابلیت RSS Reader در اندروید دیگر کاربران شما مجبور نیستند که برای مشاهده ی هر گونه بروزرسانی در وب سایت شما به آن مراجعه کنند، بلکه می توانند به راحتی از طریق اپلیکیشن شما این بروزرسانی ها مشاهده کنند.
در این مبحث ما شما را با ساختار کلی استفاده از RSS Reader در اندروید و متدهای مربوطه آشنا می کنیم.
در ادامه ی آموزش نیز به ارائه ی یک مثال از بکارگیری RSS Reader در برنامه نویسی اندروید می پردازیم.
RSS یک سند است که توسط وب سایت با افزونه xml. ایجاد شده است.
شما می توانید این سند را به راحتی تجزیه کنید و آن را به کاربر در برنامه خود نشان دهید. یک سند RSS همانند نمونه زیر است:
1 2 3 4 5 6 7 | <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 همانند نمونه ی فوق، شامل عناصر زیر است:
ردیف | کمپوننت و توضیحات مربوطه |
---|---|
۱ | channel این عنصر برای توصیف RSS استفاده می شود. |
۲ | title عنوان کانال را تعیین می کند. |
۳ | link لینک دسترسی به کانال را تعیین می کند. |
۴ | description کانال را توصیف می کند. |
ارسال یک سند RSS همانند ارسال XML می باشد.
برای این منظور ابتدا یک XMLPullParser را ایجاد می کنیم، اما برای ایجاد این ابتدا XmlPullParserFactory شی را ایجاد می کنیم.
سپس متد () newPullParser را برای ایجاد XMLPullParser فراخوانی می کنیم:
1 2 | private XmlPullParserFactory xmlFactoryObject = XmlPullParserFactory.newInstance(); private XmlPullParser myparser = xmlFactoryObject.newPullParser(); |
گام بعدی شامل تعیین پرونده برای XmlPullParser است که حاوی XML است.
این می تواند یک فایل باشد یا یک Stream باشد، که برای یک Stream ساختار آن به صورت زیر است:
1 | myparser.setInput(stream, null); |
در مرحله ی نهایی XML. را که یک فایل XML شامل رویدادها ، نام ، Text و AttributesValue و… می باشد را ارسال می کنیم.
بنابراین XMLPullParser دارای یک تابع جداگانه برای تجزیه هر یک از اجزای فایل XML است.
این ساختار به صورت زیر است:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | 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 نام تگ را مشخص می کند که در این مورد ما نام آن را temperature قرار می دهیم.
ما متد getAttributeValue را برای برگرداندن مقداری که در تگ temperature استفاده شده، فراخوانی می کنیم.
متدهای دیگری نیز توسط این کلاس برای ارسال بهتر XML ارائه شده که به شرح زیر می باشد:
ردیف | متد و توضیحات |
---|---|
۱ | getAttributeCount() این متد فقط تعداد صفات تگ start جاری را برمی گرداند. |
۲ | getAttributeName(int index) این متد نام یک صفت را که با یک مقدار index مشخص می شود را برمی گرداند. |
۳ | getColumnNumber() این متد شماره ستون جاری را که از ۰ شروع می شود برمی گرداند. |
۴ | getDepth() این متد عمق عنصر جاری را برمی گرداند. |
۵ | getLineNumber() این متد شماره خط جاری را که از ۱ شروع می شود برمی گرداند. |
۶ | getNamespace() name space عنصر جاری را برمی گرداند. |
۷ | getPrefix() این روش پیشوند عنصر فعلی را باز می کند. |
۸ | getName() این متد نام تگ را برمی گرداند. |
۹ | getText() یک text را برای یک عنصر خاص برمی گرداند. |
۱۰ | isWhitespace() این متد بررسی می کند که آیا رویداد TEXT فعلی شامل تنها فضای سفید است یا خیر. |
در این مثال در قالب یک برنامه ما چگونگی استفاده از کلاس XMLPullParser را به شما ارائه خواهیم داد.
در این مثال یک اپلیکیشن ارسال ساده را ایجاد می کنیم که شما یک سند RSS را به آدرس pvlearn.com/android/sampleXML.xml ارسال می کنیم.
شما می توانید این برنامه را در یک دستگاه اندروید واقعی و یا در شبیه ساز اندروید اجرا کنید.
مراحل انجام کار:
ردیف | توضیحات |
---|---|
۱ | مراحل ایجاد برنامه در اندروید استدیو را طبق بخش های قبلی تحت عنوان پکیج com.example.Pvlearn.myapplication دنبال کنید. |
۲ | به فایل src/MainActivity.java کدهای لازم را اضافه کنید. |
۳ | طراحی مناسب با این برنامه را به res/layout/activity_main اضافه کنید. |
۴ | یک فایل جاوا در زیر src/HandleXML.java برای fetch کردن و ارسال داده xml ایجاد کنید. |
۵ | یک فایل جاوا برای نمایش xml ایجاد کنید. |
۵ | به محتوای فایل AndroidManifest.xml مجوزهای اینترنت را اضافه کنید. |
۶ | برنامه را اجرا کنید تا دستگاه اندروید شما نتیجه را نمایش دهد. |
محتوای main activity در src/MainActivity.java :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 | package com.example.pvlearn.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="https://pvlearn.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); } }); } } |
محتوای فایل جاوای src/HandleXML.java:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 | 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(); } } |
یک فایل جاوا با نام second.java در زیر دایرکتوری java/second.java با محتوای زیر ایجاد کنید:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | package com.example.pvlearn.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("https://pvlearn.com/android/sampleXML.xml"); } } |
یک فایل xml ایجاد کرده و محتوای زیر را در آن قرار دهید:
1 2 3 4 5 6 7 8 9 10 11 | <?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 :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 | <?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 :
1 2 3 | <resources> <string name="app_name">My Application</string> </resources> |
محتوای پیش فرض فایل AndroidManifest.xml.:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.pvlearn.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> |
برای اجرای برنامه ی اندرویدی خود یکی از فایل های activity در برنامه ی خود کلیک کنید.
سپس روی نماد (Run) از نوار ابزار برای شروع اجرای برنامه کلیک کنید.
صفحه اول این اپلیکیشن به صورت زیر ظاهر خواهد شد:
حالا دکمه Fetch را لمس کنید تا RSS واکشی شود، سپس صفحه زیر ظاهر شده که نتیجه Fetch داده ها را برمی گرداند.
در نهایت دکمه روی result کلیک کرده و یا ضربه بزنید تا کدهای XML در مسیر https://pvlearn.com/android/sampleXML.xml را مشاهده کنید.
اگر می خواهید که کاربران شما بتوانند بروزرسانی ها روزمره ی وب سایت شما را از طریق اپلیکیشن شما مشاهده کنند، می توانید از RSS Reader در برنامه نویسی اندروید خود به صورتیکه در مباحث فوق اشاره شد، استفاده کنید.
در بخش بعدی به آموزش های Screen cast در اندروید می پردازیم.