- Saved searches
- Use saved searches to filter your results more quickly
- isberg1/Android-RSSv2-reader
- Name already in use
- Sign In Required
- Launching GitHub Desktop
- Launching GitHub Desktop
- Launching Xcode
- Launching Visual Studio Code
- Latest commit
- Git stats
- Files
- README.md
- Android — RSS Reader
- RSS Example
- RSS Elements
- Parsing RSS
- Example
Saved searches
Use saved searches to filter your results more quickly
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session. You switched accounts on another tab or window. Reload to refresh your session.
isberg1/Android-RSSv2-reader
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Sign In Required
Please sign in to use Codespaces.
Launching GitHub Desktop
If nothing happens, download GitHub Desktop and try again.
Launching GitHub Desktop
If nothing happens, download GitHub Desktop and try again.
Launching Xcode
If nothing happens, download Xcode and try again.
Launching Visual Studio Code
Your codespace will open once ready.
There was a problem preparing your codespace, please try again.
Latest commit
Git stats
Files
Failed to load latest commit information.
README.md
originally an assignment in the NTNU course IMT3673 2019
This is a simple RSS news reader using rss2.0. It has been tested on the rss feeds form:
the app consists of 2 Activities. MainActivity that uses 2 fragments, List and preferences. and a WebActivity for displaying websites. A SQLite database to store rss articles. One background service that downloads rss articles and stores them in a database.
in the list fragment you may read the tittle, publication date and descriptions of rss articles. by long-clicking on a item in the list the app will open the rss link in a web browser over the rss list is a edit text field, where you may search all the articles stored in the database based on a regex pattern. the button next to th field activates the search. A empty search will refresh the rss list(by checking the database for new entries).
In the preference fragment you may specify the following things for the apps behavior.
- the source of the rss feed to be displayed in the list fragment.
- how often the background service is to run
- how many entries to be displayed in the list fragment
- a editText field to add new rss source url
- a button for applying changes
invalid entries are rejected, and will result in a snackBar displaying an error message.
a simple WebView that uses a url it gets as a parameter to open a website.
The app is organized by a tab layout. in order to move between the fragments you may push one of the tabs, or you may swipe left of right.
the list fragment is made using a recyclerView with a card layout. In order to perform the required regex search. the app extract entries from the database and does a regex match in java. I originally wanted to perform the regex search in the database directly. but found that SQLite does not support such functionality. it has a function for searching for a word pattern by using «LIKE %pattern%», but this would not be a fully functional regex search. clicking on a card , long or short, will open WebActivity.
i have only tested the app for use with rss2.0. adding a new entry to the rss url source will also add it to a list of url that a background service monitors for new content. in order to apply any changes you must press the «Apply Changes» button. only then will the app try to validate and activate the changes. preferences are stored in DefaultSharedPreferences. this is because it is the recommended way to do it in android. and because DefaultSharedPreferences are easily accessible to all activities, fragments and classes.
for the database implementation i used the ROOM feature of SQLite. the app only uses one table that look like this:
link (primary key), title, pubDate, description, origin, sortValue, imageUrl
about the background service
the background service is implemented as a android jobservice, using jobScheduler. this allows the service to run only if several criteria are met. criteria like time or if network is available. the drawback of using this method is that android may not run the service exactly when it is suppose to. for example you may specify the app to run every 30 minutes. but android may run it every 29, 31, 33 minutes or their about in stead. also the minimum time between runs are 15 minutes.
all tests are located in Android-RSSv2-reader/app/src/androidTest/java/com/android_lab_2/ExampleInstrumentedTest.java
in order to parse the rss XML feed. i use XML pull parsing. I originally thought about using XML DOM paring instead. by i found that this is so memory consuming it is not the recommended way to do XML paring in android.
- The git repository URL is correctly provided, such that command works: git clone
- The code is well, logically organised and structured into appropriate classes. Everything should be in a single package.
- It is clear to the user what RSS feed formats are supported (RSS2.0 and/or Atom)
- The user can go to Preferences and set the URL of the RSS feed.
- The user can go to Preferences and set the feed item limit.
- The user can go to Preferences and set the feed refresh frequency.
- The user can see the list of items from the feed on the home Activity ListView.
- The user can go to a particular item by clicking on it. The content will be displayed in newly open activity. The back button puts the user back onto the main ListView activity to select another item.
- The user can press the back button from the main activity to quit the app.
- When the content article has graphics, it is rendered correctly.
- The Filter EditText works as expected.
- The app has JUnit Tests for testing the parsing, and the filtering functionality.
Android — RSS Reader
RSS stands for Really Simple Syndication. RSS is an easy way to share your website updates and content with your users so that users might not have to visit your site daily for any kind of updates.
RSS Example
RSS is a document that is created by the website with .xml extension. You can easily parse this document and show it to the user in your application. An RSS document looks like this.
http://www.google.com World's best search engine
RSS Elements
An RSS document such as above has the following elements.
This element is used to describe the RSS feed
Defines the title of the channel
Defines the hyper link to the channel
Parsing RSS
Parsing an RSS document is more like parsing XML. So now lets see how to parse an XML document.
For this, We will create XMLPullParser object , but in order to create that we will first create XmlPullParserFactory object and then call its newPullParser() method to create XMLPullParser. Its syntax is given below −
private XmlPullParserFactory xmlFactoryObject = XmlPullParserFactory.newInstance(); private XmlPullParser myparser = xmlFactoryObject.newPullParser();
The next step involves specifying the file for XmlPullParser that contains XML. It could be a file or could be a Stream. In our case it is a stream.Its syntax is given below −
myparser.setInput(stream, null);
The last step is to parse the XML. An XML file consist of events , Name , Text , AttributesValue e.t.c. So XMLPullParser has a separate function for parsing each of the component of XML file. Its syntax is given below −
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(); >
The method getEventType returns the type of event that happens. e.g: Document start, tag start e.t.c. The method getName returns the name of the tag and since we are only interested in temperature, so we just check in conditional statement that if we got a temperature tag, we call the method getAttributeValue to return us the value of temperature tag.
Apart from the these methods, there are other methods provided by this class for better parsing XML files. These methods are listed below −
This method just Returns the number of attributes of the current start tag.
getAttributeName(int index)
This method returns the name of the attribute specified by the index value.
This method returns the Returns the current column number, starting from 0.
This method returns Returns the current depth of the element.
Returns the current line number, starting from 1.
This method returns the name space URI of the current element.
This method returns the prefix of the current element.
This method returns the name of the tag.
This method returns the text for that particular element.
This method checks whether the current TEXT event contains only white space characters.
Example
Here is an example demonstrating the use of XMLPullParser class. It creates a basic Parsing application that allows you to parse an RSS document present here at /android/sampleXML.xml and then show the result.
To experiment with this example, you can run this on an actual device or in an emulator.
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. |
Following is the content of the modified main activity file 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="https://www.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); >>); > >
Following is the content of the java file 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(); > >
Create a file and named as second.java file under directory 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("https://www.tutorialspoint.com/android/sampleXML.xml"); >>
Create a xml file at res/layout/second_activity.xml
Modify the content of res/layout/activity_main.xml to the following −
Modify the res/values/string.xml to the following
This is the default AndroidManifest.xml.
Let’s try to run your application. I assume you had created your AVD while doing environment setup. To run the app from Android studio, open one of your project’s activity files and click Run icon from the toolbar. Android studio installs the app on your AVD and starts it and if everything is fine with your setup and application, it will display following Emulator window −
Just press the Fetch Feed button to fetch RSS feed. After pressing, following screen would appear which would show the RSS data.