Advertisement
Guest User

Untitled

a guest
Mar 23rd, 2019
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.52 KB | None | 0 0
  1. package com.example.imt3673_lab_2;
  2.  
  3. import android.content.Context;
  4. import android.os.AsyncTask;
  5. import android.support.v4.widget.SwipeRefreshLayout;
  6. import android.support.v7.app.AppCompatActivity;
  7. import android.os.Bundle;
  8. import android.support.v7.widget.LinearLayoutManager;
  9. import android.support.v7.widget.RecyclerView;
  10. import android.text.TextUtils;
  11. import android.util.Log;
  12. import android.util.Xml;
  13. import android.widget.Button;
  14. import android.widget.EditText;
  15. import android.widget.TextView;
  16. import android.widget.Toast;
  17.  
  18. import org.xmlpull.v1.XmlPullParser;
  19. import org.xmlpull.v1.XmlPullParserException;
  20.  
  21. import java.io.IOException;
  22. import java.io.InputStream;
  23. import java.net.URL;
  24. import java.util.ArrayList;
  25. import java.util.List;
  26.  
  27. public class MainActivity extends AppCompatActivity {
  28.  
  29. private RecyclerView mRecyclerView;
  30. private EditText mEditText;
  31. private Button mFetchFeedButton;
  32. private SwipeRefreshLayout mSwipeLayout;
  33. private TextView mFeedTitleTextView;
  34. private TextView mFeedLinkTextView;
  35. private TextView mFeedDescriptionTextView;
  36.  
  37. private ArrayList<RssFeedModel> mFeedModelList;
  38. private String mFeedTitle;
  39. private String mFeedLink;
  40. private String mFeedDescription;
  41. @Override
  42. protected void onCreate(Bundle savedInstanceState) {
  43. super.onCreate(savedInstanceState);
  44. setContentView(R.layout.activity_main);
  45.  
  46. mRecyclerView = (RecyclerView) findViewById(R.id.recyclerView);
  47. mEditText = (EditText) findViewById(R.id.rssFeedEditText);
  48. mFetchFeedButton = (Button) findViewById(R.id.fetchFeedButton);
  49. mSwipeLayout = (SwipeRefreshLayout) findViewById(R.id.swipeRefreshLayout);
  50. mFeedTitleTextView = (TextView) findViewById(R.id.feedTitle);
  51. mFeedDescriptionTextView = (TextView) findViewById(R.id.feedDescription);
  52. mFeedLinkTextView = (TextView) findViewById(R.id.feedLink);
  53.  
  54. mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
  55.  
  56. mFetchFeedButton.setOnClickListener(v -> {
  57. new FetchFeedTask(MainActivity.this).execute((Void) null);
  58. });
  59.  
  60. mSwipeLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener(){
  61. @Override
  62. public void onRefresh(){
  63. new FetchFeedTask(MainActivity.this).execute((Void) null);
  64. }
  65. });
  66. }
  67.  
  68. public void FetchTheURL() {
  69. new FetchFeedTask(MainActivity.this).execute((Void) null);
  70. }
  71.  
  72. public ArrayList<RssFeedModel> parseFeed(InputStream inputStream) throws XmlPullParserException,
  73. IOException {
  74. String title = null;
  75. String link = null;
  76. String description = null;
  77. String img = null;
  78. boolean isItem = false;
  79. ArrayList<RssFeedModel> items = new ArrayList<>();
  80.  
  81. try {
  82. XmlPullParser xmlPullParser = Xml.newPullParser();
  83. xmlPullParser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, false);
  84. xmlPullParser.setInput(inputStream, null);
  85.  
  86. xmlPullParser.nextTag();
  87. while (xmlPullParser.next() != XmlPullParser.END_DOCUMENT) {
  88. int eventType = xmlPullParser.getEventType();
  89.  
  90. String name = xmlPullParser.getName();
  91. if(name == null)
  92. continue;
  93.  
  94. if(eventType == XmlPullParser.END_TAG) {
  95. if(name.equalsIgnoreCase("item")) {
  96. isItem = false;
  97. }
  98. continue;
  99. }
  100.  
  101. if (eventType == XmlPullParser.START_TAG) {
  102. if(name.equalsIgnoreCase("item")) {
  103. isItem = true;
  104. continue;
  105. }
  106. }
  107.  
  108. Log.d("MyXmlParser", "Parsing name ==> " + name);
  109. String result = "";
  110. String attr = "";
  111. if (xmlPullParser.next() == XmlPullParser.TEXT) {
  112. result = xmlPullParser.getText();
  113. xmlPullParser.nextTag();
  114. }
  115.  
  116. if (name.equalsIgnoreCase("title")) {
  117. title = result;
  118. } else if (name.equalsIgnoreCase("link")) {
  119. link = result;
  120. } else if (name.equalsIgnoreCase("description")) {
  121. description = result;
  122. } else if (name.equalsIgnoreCase("encolosure")){
  123. attr = xmlPullParser.getAttributeValue(xmlPullParser.getNamespace(), "url");
  124. img = attr;
  125. }
  126.  
  127. if (title != null && link != null && description != null) {
  128. if(isItem) {
  129. RssFeedModel item = new RssFeedModel(title, link, description, img);
  130. items.add(item);
  131. }
  132. else {
  133. mFeedTitle = title;
  134. mFeedLink = link;
  135. mFeedDescription = description;
  136. }
  137.  
  138. title = null;
  139. link = null;
  140. description = null;
  141. img = null;
  142. isItem = false;
  143. }
  144. }
  145.  
  146. return items;
  147. } finally {
  148. inputStream.close();
  149. }
  150. }
  151.  
  152.  
  153. private class FetchFeedTask extends AsyncTask<Void, Void, Boolean> {
  154.  
  155. private String urlLink;
  156. private Context context;
  157.  
  158. private FetchFeedTask(Context context) {
  159. this.context = context.getApplicationContext();
  160. }
  161.  
  162. @Override
  163. protected void onPreExecute() {
  164. mFeedTitle = null;
  165. mFeedLink = null;
  166. mFeedDescription = null;
  167. /*
  168. //when we have load with fetch we get the value from a preference
  169. SharedPreferences rssURL;
  170. rssURL = getSharedPreferences(Preferences.URL, MODE_PRIVATE);
  171. urlLink = rssURL.getString("url", "");
  172. */
  173. }
  174.  
  175. @Override
  176. protected Boolean doInBackground(Void... voids) {
  177. if (TextUtils.isEmpty(urlLink)) {
  178. return false;
  179. }
  180. try {
  181. if(!urlLink.startsWith("http://") && !urlLink.startsWith("https://"))
  182. urlLink = "https://" + urlLink;
  183.  
  184. URL url = new URL(urlLink);
  185. InputStream inputStream = url.openConnection().getInputStream();
  186. mFeedModelList = parseFeed(inputStream);
  187. return true;
  188. } catch (IOException e) {
  189. Log.d("i", "Error", e);
  190. } catch (XmlPullParserException e) {
  191. Log.d("i", "Error", e);
  192. }
  193. return false;
  194. }
  195.  
  196. @Override
  197. protected void onPostExecute(Boolean success) {
  198.  
  199. if (success) {
  200. mFeedTitleTextView.setText("Feed Title: " + mFeedTitle);
  201. mFeedDescriptionTextView.setText("Feed Description: " + mFeedDescription);
  202. mFeedLinkTextView.setText("Feed Link: " + mFeedLink);
  203. // Fill RecyclerView
  204. mRecyclerView.setAdapter(new RssFeedListAdapter(mFeedModelList));
  205. } else {
  206. Toast.makeText(MainActivity.this,
  207. "Enter a valid Rss feed url",
  208. Toast.LENGTH_LONG).show();
  209. }
  210. }
  211. }
  212.  
  213. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement