D
D
Daniel2018-06-11 15:25:45
Java
Daniel, 2018-06-11 15:25:45

How to make a news feed from an RSS feed in an application?

There is an RSS feed. It is parsed and saved into an array, how to display the title, description and picture from the array in RecycleView. With ListView, something similar happened to me, but it doesn’t work with RecycleView.
MainActivity code -

spoiler
public InputStream getInputStream (URL url)
    {
        try {
            return url.openConnection().getInputStream();
        } catch (IOException e) {
            return null;
        }
    }

    private class ProcessInBackground extends AsyncTask<Integer, Void, Exception> {

        ProgressDialog progressDialog = new ProgressDialog(MainActivity.this);

        Exception exception = null;

        @Override
        protected void onPreExecute() {
            super.onPreExecute();

        }

        @Override
        protected Exception doInBackground(Integer... params) {

            try {
                url = new URL(HHMurl);
                XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
                factory.setNamespaceAware(false);
                XmlPullParser xpp = factory.newPullParser();
                xpp.setInput(getInputStream(url), "UTF_8");
                boolean insideItem = false;
                int eventType = xpp.getEventType();

                while (eventType != XmlPullParser.END_DOCUMENT) {
                    if (eventType == XmlPullParser.START_TAG) {
                        if (xpp.getName().equalsIgnoreCase("item")) {
                            insideItem = true;
                        } else if (xpp.getName().equalsIgnoreCase("title")) {
                            if (insideItem) {
                                titles.add(xpp.nextText());
                            }
                        } else if (xpp.getName().equalsIgnoreCase("link")) {
                            if (insideItem) {
                                links.add(xpp.nextText());
                            }
                        } else if (xpp.getName().equalsIgnoreCase("content:encoded")) {
                            if (insideItem) {
                                description.add(xpp.nextText());
                            }
                        } else if (xpp.getName().equalsIgnoreCase("pubDate")) {
                            if (insideItem) {
                                pubDate.add(xpp.nextText());
                            }
                        } else if (xpp.getName().equalsIgnoreCase("title2")) {
                            if (insideItem) {
                                titlesForFullPost.add(xpp.nextText());
                            }
                        }
                    } else if (eventType == XmlPullParser.END_TAG && xpp.getName().equalsIgnoreCase("item")) {
                        insideItem = false;
                    }

                    eventType = xpp.next();
                }


            } catch (XmlPullParserException | IOException e) {
                exception = e;
            }

            return exception;
        }

        @Override
        protected void onPostExecute(Exception s) {
            super.onPostExecute(s);

            ArrayAdapter<String> adapter = new ArrayAdapter<>(MainActivity.this, R.layout.sw_main_view, titles);

            SWposts.setAdapter(adapter);
             progressDialog.dismiss();
        }
    }

Answer the question

In order to leave comments, you need to log in

1 answer(s)
L
lahomie93, 2018-06-11
@lahomie93

check out my app code on github. The application is called RSS reader. I also have Bbc reader on my forks, also a good example of an application for reading RSS feeds. https://github.com/LAHomieJob/RSS-Reader

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question