RSS Reader در برنامه نویسی اندروید

4 سال پیش

RSS Reader در برنامه نویسی اندروید

در این درس از مجموعه آموزش برنامه نویسی سایت سورس باران، در مورد RSS Reader در برنامه نویسی اندروید صحبت خواهیم کرد.

 

RSS مخفف Really Simple Syndication است. RSS روشی آسان برای به اشتراک گذاشتن به روزرسانی ها و مطالب وب سایت شما با کاربران است تا کاربران مجبور نباشند روزانه از سایت شما برای هر نوع به روزرسانی بازدید کنند.

مثالی از RSS Reader در برنامه نویسی اندروید

RSS سندی است که توسط وب سایت با پسوند .xml ایجاد می شود.

به راحتی می توانید این سند را تجزیه کرده و در برنامه خود به کاربر نشان دهید.

یک سند 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  دارای عناصر زیر است.

  • channel

این عنصر برای توصیف خوراک RSS استفاده می شود

  • title

عنوان کانال را تعریف می کند

  •  link

هایپر لینک کانال را مشخص می کند

  •  description

کانال را توصیف می کند

 

تجزیه RSS

تجزیه یک سند RSS بیشتر شبیه تجزیه XML است.

بنابراین اکنون می خواهیم نحوه تجزیه یک سند XML را ببینیم.

برای این کار ما شی object XMLPullParser را ایجاد خواهیم کرد، اما برای ایجاد آن ابتدا شی object XmlPullParserFactory را ایجاد می کنیم و سپس برای ایجاد XMLPullParser متد ()newPullParser  آن را فراخوانی می کنیم.

نحو آن در زیر آورده شده است:

private XmlPullParserFactory xmlFactoryObject = XmlPullParserFactory.newInstance();
private XmlPullParser myparser = xmlFactoryObject.newPullParser();

مرحله بعدی شامل مشخص کردن فایلی برای XmlPullParser است که حاوی XML است. این می تواند یک فایل باشد یا می تواند یک جریان باشد.

در مورد ما این یک جریان است. نحو آن در زیر آورده شده است:

myparser.setInput(stream, null);

آخرین مرحله تجزیه XML است. یک فایل XML از رویدادها ، نام ، متن ، 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 نام تگ را برمی گرداند و از آنجایی که ما فقط به temperature علاقه داریم، بنابراین ما فقط در عبارت شرطی بررسی می کنیم که اگر تگ temperature گرفتیم، متد getAttributeValue را فراخوانی می کنیم تا مقدار تگ temperature را به ما برگرداند.

جدا از این متدها، متدهای دیگری نیز برای تجزیه بهتر فایلهای XML توسط این کلاس ارائه شده است. این متد ها در زیر ذکر شده است:

  • ()getAttributeCount 

این متد فقط تعداد ویژگی های برچسب شروع فعلی را برمی گرداند.

  • (getAttributeName(int index

این متد نام ویژگی مشخص شده با مقدار ایندکس را برمی گرداند.

  • ()getColumnNumber 

این متد مقدار ستون فعلی را برمی گرداند ، از ۰ شروع می شود.

  • ()getDepth

این متد  عمق فعلی عنصر را برمی گرداند.

  • ()getLineNumber 

شماره خط فعلی را با شروع از ۱ برمی گرداند.

  • ()getNamespace 

این متد نام فضای URI عنصر فعلی را برمی گرداند.

  • ()getPrefix 

این متد پیشوند عنصر فعلی را برمی گرداند.

  • ()getName 

این متد نام تگ را برمی گرداند.

  • ()getText

این متد متن را برای یک عنصر خاص برمی گرداند.

()isWhitespace 

این متد بررسی می کند که آیا رویداد TEXT فعلی فقط شامل کاراکتر های فضای سفید است.

 

مثال

در اینجا مثالی برای استفاده از کلاس XMLPullParser آورده شده است. این برنامه به شما امکان می دهد یک سند RSS موجود در اینجا در /android/sampleXML.xml را تجزیه کنید و سپس نتیجه را نشان دهید.

برای آزمایش این مثال، می توانید آن را روی دستگاه واقعی یا شبیه ساز اجرا کنید.

شرح مراحل 

۱- شما برای ایجاد یک برنامه اندروید تحت یک پکیج com.example.sairamkrishna.myapplication از Android studio استفاده خواهید کرد.
۲- برای اضافه کردن کد لازم ، فایل src / MainActivity.java را تغییر دهید.
۳- res / layout / activity_main را تغییر دهید تا مولفه های XML مربوطه اضافه شود.
۴- برای واکشی و تجزیه داده های XML ، یک فایل جاوا جدید در src / HandleXML.java ایجاد کنید.
۵- برای نمایش نتیجه XML ، یک فایل جاوا جدید در src / second.java ایجاد کنید
۵- AndroidManifest.xml را تغییر دهید تا اجازه اینترنت لازم را اضافه کنید.
۶- برنامه را اجرا کنید و دستگاه اندرویدی در حال اجرا را انتخاب کنید و برنامه را روی آن نصب کنید و نتایج را بررسی کنید.

در زیر محتوای پرونده فعالیت اصلی تغییر یافته 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://tutorialspoint.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(); 
   }
}

یک فایل ایجاد کنید و به عنوان پرونده second.java در فهرست 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://tutorialspoint.com/android/sampleXML.xml");
   }
}

یک فایل xml در res / layout / second_activity.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 studio، یکی از پرونده های فعالیت پروژه خود را باز کنید و از نوار ابزار روی Android StudioRun Icon کلیک کنید. Android Studio برنامه را روی AVD شما نصب کرده و شروع به کار می کند و اگر با تنظیمات و برنامه هایتان همه چیز خوب باشد، زیر پنجره شبیه ساز نمایش داده می شود.

 

برای واکشی RSS کافیست دکمه Fetch Feed را فشار دهید. پس از فشار دادن، صفحه زیر ظاهر می شود که داده های RSS را نشان می دهد

برای دیدن XML، که در http://tutorialspoint.com/android/sampleXML.xml قرار داده شده است، فقط دکمه result را فشار دهید

 

منبع.

لیست جلسات قبل آموزش برنامه نویسی اندروید

  1. نگاهی کلی به برنامه نویسی اندروید
  2. تنظیمات محیط  در برنامه نویسی اندروید
  3. معماری برنامه نویسی اندروید
  4. اجزای برنامه در برنامه نویسی اندروید
  5. Hello World در برنامه نویسی اندروید
  6. سازماندهی و دسترسی به منابع در برنامه نویسی اندروید
  7. فعالیت در برنامه نویسی اندروید
  8. سرویس در برنامه نویسی اندروید
  9. گیرنده های پخش در برنامه نویسی اندروید
  10. ارائه دهنده محتوا در برنامه نویسی اندروید
  11. قطعات در برنامه نویسی اندروید
  12. Intent و فیلتر در برنامه نویسی اندروید
  13. طرح بندی رابط کاربری در برنامه نویسی اندروید
  14. کنترل های رابط کاربری در برنامه نویسی اندروید
  15. مدیریت رویدادها در برنامه نویسی اندروید
  16. استایل و تم ها در برنامه نویسی اندروید
  17. اجزای سفارشی در برنامه نویسی اندروید
  18. کشیدن و رها کردن در برنامه نویسی اندروید
  19. نوتیفیکیشن ها در برنامه نویسی اندروید
  20. سرویس های مبتنی بر مکان در برنامه نویسی اندروید
  21. ارسال ایمیل در برنامه نویسی اندروید
  22. ارسال پیامک در برنامه نویسی اندروید
  23. تماس های تلفنی در برنامه نویسی اندروید
  24. انتشار برنامه اندروید
  25. آموزش Alert Dialog در برنامه نویسی اندروید
  26. انیمیشن در برنامه نویسی اندروید
  27. ضبط صدا در برنامه نویسی اندروید
  28. مدیریت صدا در برنامه نویسی اندروید
  29. آموزش Auto Complete در برنامه نویسی اندروید
  30. بهترین شیوه ها در برنامه نویسی اندروید
  31. بلوتوث در برنامه نویسی اندروید
  32. استفاده از دوربین در برنامه نویسی اندروید
  33. کلیپ بورد در برنامه نویسی اندروید
  34. فونت سفارشی در برنامه نویسی اندروید
  35. پشتیبان گیری از داده ها در برنامه نویسی اندروید
  36.  ابزارهای توسعه دهنده در برنامه نویسی اندروید
  37. آموزش شبیه ساز در برنامه نویسی اندروید
  38. اتصال به فیس بوک در برنامه نویسی اندروید
  39. حرکات لمسی در برنامه نویسی اندروید
  40. آموزش گوگل مپ در برنامه نویسی اندروید
  41. افکت های تصویر در برنامه نویسی اندروید
  42. Image Switcher در برنامه نویسی اندروید
  43. حافظه داخلی در برنامه نویسی اندروید
  44. آموزش کار با JetPlayer در برنامه نویسی اندروید
  45. JSON Parser در برنامه نویسی اندروید
  46. ارتباط با LinkedIn در برنامه نویسی اندروید
  47. نوار پیشرفت Spinner در برنامه نویسی اندروید
  48. بومی سازی در برنامه نویسی اندروید
  49. صفحه Login در برنامه نویسی اندروید
  50. MediaPlayer در برنامه نویسی اندروید
  51. Multitouch در برنامه نویسی اندروید
  52. Navigation در برنامه نویسی اندروید
  53. اتصال به اینترنت در برنامه نویسی اندروید
  54. NFC در برنامه نویسی اندروید
  55. PHP و MYSQL در برنامه نویسی اندروید
  56. Progress Circle دربرنامه نویسی اندروید
  57. Progress Bar در برنامه نویسی اندروید
  58. Push Notification در برنامه نویسی اندروید
  59. RenderScript در برنامه نویسی اندروید
0
برچسب ها :
نویسنده مطلب erfan molaei

دیدگاه شما

بدون دیدگاه