Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.example.imt3673_lab_2;
- import android.content.Context;
- 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.TextUtils;
- import android.util.Log;
- import android.util.Xml;
- 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;
- public class MainActivity extends AppCompatActivity {
- private RecyclerView mRecyclerView;
- private EditText mEditText;
- private Button mFetchFeedButton;
- private SwipeRefreshLayout mSwipeLayout;
- private TextView mFeedTitleTextView;
- private TextView mFeedLinkTextView;
- private TextView mFeedDescriptionTextView;
- private ArrayList<RssFeedModel> mFeedModelList;
- private String mFeedTitle;
- private String mFeedLink;
- 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);
- mFeedLinkTextView = (TextView) findViewById(R.id.feedLink);
- mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
- mFetchFeedButton.setOnClickListener(v -> {
- new FetchFeedTask(MainActivity.this).execute((Void) null);
- });
- mSwipeLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener(){
- @Override
- public void onRefresh(){
- new FetchFeedTask(MainActivity.this).execute((Void) null);
- }
- });
- }
- public void FetchTheURL() {
- new FetchFeedTask(MainActivity.this).execute((Void) null);
- }
- public ArrayList<RssFeedModel> parseFeed(InputStream inputStream) throws XmlPullParserException,
- IOException {
- String title = null;
- String link = null;
- String description = null;
- String img = null;
- boolean isItem = false;
- ArrayList<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 = "";
- String attr = "";
- if (xmlPullParser.next() == XmlPullParser.TEXT) {
- result = xmlPullParser.getText();
- xmlPullParser.nextTag();
- }
- if (name.equalsIgnoreCase("title")) {
- title = result;
- } else if (name.equalsIgnoreCase("link")) {
- link = result;
- } else if (name.equalsIgnoreCase("description")) {
- description = result;
- } else if (name.equalsIgnoreCase("encolosure")){
- attr = xmlPullParser.getAttributeValue(xmlPullParser.getNamespace(), "url");
- img = attr;
- }
- if (title != null && link != null && description != null) {
- if(isItem) {
- RssFeedModel item = new RssFeedModel(title, link, description, img);
- items.add(item);
- }
- else {
- mFeedTitle = title;
- mFeedLink = link;
- mFeedDescription = description;
- }
- title = null;
- link = null;
- description = null;
- img = null;
- isItem = false;
- }
- }
- return items;
- } finally {
- inputStream.close();
- }
- }
- private class FetchFeedTask extends AsyncTask<Void, Void, Boolean> {
- private String urlLink;
- private Context context;
- private FetchFeedTask(Context context) {
- this.context = context.getApplicationContext();
- }
- @Override
- protected void onPreExecute() {
- mFeedTitle = null;
- mFeedLink = null;
- mFeedDescription = null;
- /*
- //when we have load with fetch we get the value from a preference
- SharedPreferences rssURL;
- rssURL = getSharedPreferences(Preferences.URL, MODE_PRIVATE);
- urlLink = rssURL.getString("url", "");
- */
- }
- @Override
- protected Boolean doInBackground(Void... voids) {
- if (TextUtils.isEmpty(urlLink)) {
- return false;
- }
- try {
- if(!urlLink.startsWith("http://") && !urlLink.startsWith("https://"))
- urlLink = "https://" + urlLink;
- URL url = new URL(urlLink);
- InputStream inputStream = url.openConnection().getInputStream();
- mFeedModelList = parseFeed(inputStream);
- return true;
- } catch (IOException e) {
- Log.d("i", "Error", e);
- } catch (XmlPullParserException e) {
- Log.d("i", "Error", e);
- }
- return false;
- }
- @Override
- protected void onPostExecute(Boolean success) {
- if (success) {
- mFeedTitleTextView.setText("Feed Title: " + mFeedTitle);
- mFeedDescriptionTextView.setText("Feed Description: " + mFeedDescription);
- mFeedLinkTextView.setText("Feed Link: " + mFeedLink);
- // Fill RecyclerView
- mRecyclerView.setAdapter(new RssFeedListAdapter(mFeedModelList));
- } else {
- Toast.makeText(MainActivity.this,
- "Enter a valid Rss feed url",
- Toast.LENGTH_LONG).show();
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement