package com.example.anmol.rssdemo;

 

import .content.Intent;

import android.os.AsyncTask;

import android.support.v4.widget.SwipeRefreshLayout;

import android.support.v7.app.AppCompatActivity;

import android.os.Bundle;

import android.support.v7.widget.LinearLayoutManager;

import android.support.v7.widget.RecyclerView;

import android.text.Html;

import android.text.TextUtils;

import android.util.Log;

import android.util.Xml;

import android.view.LayoutInflater;

import android.view.View;

import android.view.ViewGroup;

import android.webkit.WebView;

import android.widget.Button;

import android.widget.EditText;

import android.widget.TextView;

import android.widget.Toast;

 

import org.xmlpull.v1.XmlPullParser;

import org.xmlpull.v1.XmlPullParserException;

 

import java.io.IOException;

import java.io.InputStream;

import java.net.URL;

import java.util.ArrayList;

import java.util.List;

import java.util.regex.Matcher;

import java.util.regex.Pattern;

 

public class MainActivity extends AppCompatActivity {

 

    private RecyclerView mRecyclerView;

    private EditText mEditText;

    private Button mFetchFeedButton;

    private SwipeRefreshLayout mSwipeLayout;

    private TextView mFeedTitleTextView;

    private TextView mFeedDescriptionTextView;

 

    private List<RssFeedModel> mFeedModelList;

    private String mFeedTitle;

    private String mFeedContent;

    private String mFeedDescription;

 

    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

 

        mRecyclerView = (RecyclerView) findViewById(R.id.recyclerView);

        mEditText = (EditText) findViewById(R.id.rssFeedEditText);

        mFetchFeedButton = (Button) findViewById(R.id.fetchFeedButton);

        mSwipeLayout = (SwipeRefreshLayout) findViewById(R.id.swipeRefreshLayout);

        mFeedTitleTextView = (TextView) findViewById(R.id.feedTitle);

        mFeedDescriptionTextView = (TextView) findViewById(R.id.feedDescription);

 

 

        mRecyclerView.setLayoutManager(new LinearLayoutManager(this));

 

        mFetchFeedButton.setOnClickListener(new View.OnClickListener() {

            @Override

            public void onClick(View view) {

                new FetchFeedTask().execute((Void) null);

            }

        });

        mSwipeLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {

            @Override

            public void onRefresh() {

                new FetchFeedTask().execute((Void) null);

            }

        });

    }

    private class FetchFeedTask extends AsyncTask<Void, Void, Boolean> {

 

        private String urlLink;

 

        @Override

        protected void onPreExecute() {

            mSwipeLayout.setRefreshing(true);

            urlLink = mEditText.getText().toString();

        }

 

        @Override

        protected Boolean doInBackground(Void... voids) {

            if (TextUtils.isEmpty(urlLink))

                return false;

 

            try {

                if(!urlLink.startsWith(“http://”) && !urlLink.startsWith(“https://”))

                    urlLink = “http://” + urlLink;

 

                URL url = new URL(urlLink);

                InputStream inputStream = url.openConnection().getInputStream();

                mFeedModelList = parseFeed(inputStream);

                return true;

            } catch (IOException e) {

                e.printStackTrace();

            } catch (XmlPullParserException e) {

                e.printStackTrace();

            }

            return false;

        }

 

        @Override

        protected void onPostExecute(Boolean success) {

            mSwipeLayout.setRefreshing(false);

 

            if (success) {

                mFeedTitleTextView.setText(“Feed Title: “ + mFeedTitle);

                mFeedDescriptionTextView.setText(“Feed Description: “ + mFeedDescription);

                // Fill RecyclerView

                mRecyclerView.setAdapter(new RssFeedListAdapter(mFeedModelList));

 

            } else {

                Toast.makeText(MainActivity.this,

                        “Enter a valid feed url”,

                        Toast.LENGTH_LONG).show();

            }

        }

    }

 

    public List<RssFeedModel> parseFeed(InputStream inputStream) throws XmlPullParserException,

            IOException {

        String title = null;

        String content = null;

        String description = null;

        boolean isItem = false;

        List<RssFeedModel> items = new ArrayList<>();

 

        try {

            XmlPullParser xmlPullParser = Xml.newPullParser();

            xmlPullParser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, false);

            xmlPullParser.setInput(inputStream, null);

 

            xmlPullParser.nextTag();

            while (xmlPullParser.next() != XmlPullParser.END_DOCUMENT) {

                int eventType = xmlPullParser.getEventType();

 

                String name = xmlPullParser.getName();

                if(name == null)

                    continue;

 

                if(eventType == XmlPullParser.END_TAG) {

                    if(name.equalsIgnoreCase(“item”)) {

                        isItem = false;

                    }

                    continue;

                }

 

                if (eventType == XmlPullParser.START_TAG) {

                    if(name.equalsIgnoreCase(“item”)) {

                        isItem = true;

                        continue;

                    }

                }

 

                Log.d(“MyXmlParser”, “Parsing name ==> “ + name);

                String result = “”;

                if (xmlPullParser.next() == XmlPullParser.TEXT) {

                    result = xmlPullParser.getText();

                    xmlPullParser.nextTag();

                }

 

                if (name.equalsIgnoreCase(“title”)) {

                    title = result;

                } else if (name.equalsIgnoreCase(“content:encoded”)) {

                    content = result;

                } else if (name.equalsIgnoreCase(“description”)) {

                    description = result;

                }

 

                if (title != null && content != null && description != null) {

                    if(isItem) {

                        RssFeedModel item = new RssFeedModel(title, content, description);

                        items.add(item);

                    }

                    else {

                        mFeedTitle = title;

                        mFeedContent = content;

                        mFeedDescription = description;

                    }

 

                    title = null;

                    content = null;

                    description = null;

                    isItem = false;

                }

            }

 

            return items;

        } finally {

            inputStream.close();

        }

    }

    public class RssFeedModel {

 

        public String title;

        public String content;

        public String description;

 

        public RssFeedModel(String title, String content, String description) {

            this.title = title;

            this.content = content;

            this.description = description;

        }

    }

 

    public class RssFeedListAdapter

            extends RecyclerView.Adapter<RssFeedListAdapter.FeedModelViewHolder> {

 

        private List<RssFeedModel> mRssFeedModels;

 

        public  class FeedModelViewHolder extends RecyclerView.ViewHolder {

            private View rssFeedView;

 

            public FeedModelViewHolder(View v) {

                super(v);

                rssFeedView = v;

            }

        }

 

        public RssFeedListAdapter(List<RssFeedModel> rssFeedModels) {

            mRssFeedModels = rssFeedModels;

        }

 

        @Override

        public FeedModelViewHolder onCreateViewHolder(ViewGroup parent, int type) {

            View v = LayoutInflater.from(parent.getContext())

                    .inflate(R.layout.recycler_layout, parent, false);

            FeedModelViewHolder holder = new FeedModelViewHolder(v);

            return holder;

        }

 

        @Override

        public void onBindViewHolder(FeedModelViewHolder holder, int position) {

            final RssFeedModel rssFeedModel = mRssFeedModels.get(position);

            ((TextView)holder.rssFeedView.findViewById(R.id.titleText)).setText(rssFeedModel.title);

 

            if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.N) {

                ((TextView)holder.rssFeedView.findViewById(R.id.descriptionText)).setText(Html.fromHtml(rssFeedModel.description,Html.FROM_HTML_MODE_LEGACY));

            } else {

                ((TextView)holder.rssFeedView.findViewById(R.id.descriptionText)).setText(Html.fromHtml(rssFeedModel.description));

            }

 

            holder.itemView.setOnClickListener(new View.OnClickListener() {

                @Override

                public void onClick(View v) {

 

                    Intent intent=new Intent(getApplicationContext(),Final.class);

                    intent.putExtra(“content”,rssFeedModel.content);

                    startActivity(intent);

 

                }

            });

 

 

               }

 

 

        @Override

        public int getItemCount() {

            return mRssFeedModels.size();

        }

    }

 

}



Source link https://www.thecrazyprogrammer.com/2018/12/android-rss--.html

LEAVE A REPLY

Please enter your comment!
Please enter your name here