Android development: How to create your own RSS reader
Coursera
If you still have the Hello AndroidPIT app project from the first article, you can now continue working with it. Otherwise, you should start a new project in Android Studio.
Rich Site Summary (RSS)
First, what is an RSS feed? Many of you read RSS feeds daily on a reader app on your computer or mobile device. To program an app that can display an RSS feed, you should know a little about the RSS itself. Wikipedia provides a good overview of the subject.
For example, the AndroidPIT RSS feed can be found here.
In short, RSS an XML file. XML made primarily for displaying text, and designed for machine readability. Our first goal is to develop an app that uses the AndroidPIT feeds XML file and display it properly in our Hello AndroidPIT app.
Representing the RSS feed
Next, I will show you how to bring the XML file to your device screen. We will take the app from the first article and modify it for our purposes.
On Android, the layout is usually defined via XML. Therefore, if you plan to devote yourself to development, it is worth learning more about XML, as it is a format that you will continually use.
The layout stored in the folder res/layout. If you open the fragment_main.xml In your project you will be able to see the XML layout and a preview of it.
After giving a id to Textviewwe have to find the ones we want to use and add them to our segment Placeholder fragment. For this we use the method findViewById. It should look like this within your method. onCreateView:
To retrieve the RSS feed at the beginning of the app, we need the following method. onStart:
@Override
public void onStart() {
super.onStart();
InputStream in = null;
try {
URL url = new URL("https://www.androidpit/feed/main.xml");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
in = conn.getInputStream();
ByteArrayOutputStream out = new ByteArrayOutputStream();
byte() buffer = new byte(1024);
for (int count; (count = in.read(buffer)) != -1; ) {
out.write(buffer, 0, count);
}
byte() response = out.toByteArray();
String rssFeed = new String(response, "UTF-8");
mRssFeed.setText(rssFeed);
} catch (IOException e) {
e.printStackTrace();
} finally {
if (in != null) {
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
If you don't understand the source code, don't worry and just copy and paste what we did. To understand it, you need to learn Java and the programming language.
If you try to run the app now, it will start compiling but will freeze at some point.
When developing apps, I need to learn how to handle crashes and what they mean. If you open the logcat At the same time as running your app, it will log all error messages issued by the app. In our case, the following appears:
com.rockylabs.androidpitrss E/AndroidRuntime FATAL EXCEPTION: main
Process: com.rockylabs.androidpitrss, PID: 14367
java.lang.RuntimeException: Unable to start activity
ComponentInfo{com.rockylabs.androidpitrss/com.rockylabs.androidpitrss.MainActivity}:
android.os.NetworkOnMainThreadException
What happened anyway? Your app tried to access the internet on the main thread, but if the request takes too long, the app stops responding and an error message is issued. To avoid this, Android has been programmed to freeze rather than run infinitely.
On Android, we need to work in a world of asynchronous workflows. This means that when a request is made to the system, it runs in the background and a notification appears to you as soon as the task is complete.
The default for all tasks is user interface thread, called UI thread. In the above case, if you try to access the internet and the server request takes too long, the interface will hang and will not proceed. As a result, you receive an error message that the application is not reacting. This warning also known as Application Not Responding (ANR). This type of message should be avoided in all ways to keep the application functional for the user.
Creative Commons Attribution 2.5
Android provides a very simple way to handle this problem in the form of a different class called AsyncTask. You can run the background task on a background thread and run the result on the UI thread.
In our app we want to run a task with AsyncTask in the background. In the previous step we have the source code for the method onStart inserted into our code. Now we need to separate the network and the user interface, which is why we created the method getAndroidPitRssFeed. This makes it easier to use the source code several times instead of spending a lot of time copying and pasting. The method getAndroidPitRssFeed the next:
If we run the app again, we will produce a stop again, but this time for lack of authorization rather than timeout. We saw no permission to access the internet and so we could not get the RSS feed.
If you have opened logcat with the application running, see the following error message:
Caused by: java.lang.SecurityException: Permission denied (missing INTERNET permission?)
To configure internet permissions, you will need to open the file AndroidManifest.xml. There you will see the following lines of code:
We will add the following below them:
And ready! If you run the application, see that it starts showing the XML of your RSS feed. As you may have noticed, it will take some time to load. The entire feed will be loaded. Later we will show you how to improve the user experience by installing a progress indicator. So far so good. You have successfully created your own RSS feed. However, nobody wants to view the XML format, which is rather raw, so we will have to clear it. Also, we can't get past the edge of the screen, and we can't scroll down at this point.
ScrollView
The solution to the above problem is to add the component Scrollview Already available on Android SDK:
In the next step, you will learn how the XML file is read, so we can modify it a bit and extract the article titles and present them in a list, so that the view is clearer.
The fruits of our work: XML shown, but without scrollability. / AndroidPIT
XML read
Our next goal is to make the title of each article show within the tags.
. If we look at the RSS feed (image above), it is inside the pore do code and right after the tag . For simplicity, we will ignore all other tags for now. Once we have worked through the entire XML file, we will better understand what they are used for.
To deal with an XML file, we need to look through the tangle of things and get the information we need from it. Fortunately for us, someone has already created a tool for this. As such, we will use a parser to extract the information we need and we will use XmlPullParser from the Android SDK.
The XML file analysis follows the following stages:
START_DOCUMENT to start the document when the parser has not read anything yet.
START_TAG what starts the parser
TEXT when the parser is busy with content
ENG_TAG when the parser is in the final tag
END_DOCUMENT when the document is finished and no further analysis options are available.
Next we have to give the parser a source to parse. Let's assign it to the string we downloaded from the server and it's being shown via TextView.
As we mentioned earlier, we want to read the RSS feed, channel, open item, and title tags to read the article title. Let's ignore everything else. So we added readRSS, read channel, ReadItem, and read title. For this we emit the following code:
private List readRss(XmlPullParser parser)
throws XmlPullParserException, IOException {
List items = new ArrayList<>();
parser.require(XmlPullParser.START_TAG, null, "rss");
while (parser.next() != XmlPullParser.END_TAG) {
if (parser.getEventType() != XmlPullParser.START_TAG) {
continue;
}
String name = parser.getName();
if (name.equals("channel")) {
items.addAll(readChannel(parser));
} else {
skip(parser);
}
}
return items;
}
Now we need to implement the read channel to read the contents, the rest we put aside:
private List readChannel(XmlPullParser parser)
throws IOException, XmlPullParserException {
List items = new ArrayList<>();
parser.require(XmlPullParser.START_TAG, null, "channel");
while (parser.next() != XmlPullParser.END_TAG) {
if (parser.getEventType() != XmlPullParser.START_TAG) {
continue;
}
String name = parser.getName();
if (name.equals("item")) {
items.add(readItem(parser));
} else {
skip(parser);
}
}
return items;
}
ReadItem Also required:
private String readItem(XmlPullParser parser)
throws XmlPullParserException, IOException {
String result = null;
parser.require(XmlPullParser.START_TAG, null, "item");
while (parser.next() != XmlPullParser.END_TAG) {
if (parser.getEventType() != XmlPullParser.START_TAG) {
continue;
}
String name = parser.getName();
if (name.equals("title")) {
result = readTitle(parser);
} else {
skip(parser);
}
}
return result;
}
We're almost there, we just need to get read title to pull the title:
// Processes title tags in the feed.
private String readTitle(XmlPullParser parser)
throws IOException, XmlPullParserException {
parser.require(XmlPullParser.START_TAG, null, "title");
String title = readText(parser);
parser.require(XmlPullParser.END_TAG, null, "title");
return title;
}
And the text on the day
:
private String readText(XmlPullParser parser)
throws IOException, XmlPullParserException {
String result = "";
if (parser.next() == XmlPullParser.TEXT) {
result = parser.getText();
parser.nextTag();
}
return result;
}
With this method, the rest of the tags will be skipped:
private void skip(XmlPullParser parser)
throws XmlPullParserException, IOException {
if (parser.getEventType() != XmlPullParser.START_TAG) {
throw new IllegalStateException();
}
int depth = 1;
while (depth != 0) {
switch (parser.next()) {
case XmlPullParser.END_TAG:
depth;
break;
case XmlPullParser.START_TAG:
depth++;
break;
}
}
}
We are now almost professional with regard to the XML parser! In the next step we will issue the item title in a list.
List Fragment
In the Android SDK there is a class called ListFragment that will help us deal with lists.
Since we already have many lines of text extracted from the XML file, we can use a standard Android function to represent the title. An adapter is a bridge type between the data we want to present and the TextViews that represent the data.
With an adapter we can use a method TextView for each title on our list. You need to use the Placeholder fragment to achieve this:
public static class PlaceholderFragment extends ListFragment {
Finally, we still need to use the AsyncTask so our analyzer can work:
If you try to run the app now, you should first see a progress indicator while the RSS feed is downloaded from the server and then a list of all feed titles will be shown as below:
Here it is! A progress indicator is the title of the XML file. / AndroidPIT
Conclusion
UFA! We know this guide is a big step compared to the first exercise. But if you have been through all the stages, you should be proud of what you have done and what you have learned. Even if you don't understand all the details, your understanding of how to program within Android will grow over time. The more you practice Android development, the more you understand about this kind of thing.
The feed you have programmed works, but it is not very useful as it currently only shows article titles. In our next exercise, we will extend the functionality and let us read the introduction of articles when we click on the title.
What do you think? What improvements would you like to see in the RSS tutorial?
. (tagsToTranslate) development android (t) rss feed (t) reader (t) app (t) app (t) program
This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish.
Read More
Cookies are small text files that can be used by websites to make a user's experience more efficient. The law states that we can store cookies on your device if they are strictly necessary for the operation of this site. For all other types of cookies we need your permission. This site uses different types of cookies. Some cookies are placed by third party services that appear on our pages.
Necessary cookies help make a website usable by enabling basic functions like page navigation and access to secure areas of the website. The website cannot function properly without these cookies.
Marketing cookies are used to track visitors across websites. The intention is to display ads that are relevant and engaging for the individual user and thereby more valuable for publishers and third party advertisers.
Preference cookies enable a website to remember information that changes the way the website behaves or looks, like your preferred language or the region that you are in.