rohithdsouza

RSS Feed Android

Sep 14th, 2021
787
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import android.app.ListActivity;
  2. import android.content.Intent;
  3. import android.net.Uri;
  4. import android.os.AsyncTask;
  5. import android.os.Bundle;
  6. import android.view.View;
  7. import android.widget.ArrayAdapter;
  8. import android.widget.ListView;
  9. import org.xmlpull.v1.XmlPullParser;
  10. import org.xmlpull.v1.XmlPullParserException;
  11. import org.xmlpull.v1.XmlPullParserFactory;
  12. import java.io.IOException;
  13. import java.io.InputStream;
  14. import java.net.MalformedURLException;
  15. import java.net.URL;
  16. import java.util.ArrayList;
  17. import java.util.List;
  18. public class MainActivity extends ListActivity
  19. {
  20.  List headlines;
  21. List links;
  22.  @Override
  23.  protected void onCreate(Bundle savedInstanceState)
  24.  {
  25.  super.onCreate(savedInstanceState);
  26.  new MyAsyncTask().execute();
  27.  }
  28.  class MyAsyncTask extends AsyncTask<Object,Void,ArrayAdapter>
  29.  {
  30.  @Override
  31.  protected ArrayAdapter doInBackground(Object[] params)
  32.  {
  33.  headlines = new ArrayList();
  34.  links = new ArrayList();
  35.  try
  36.  {
  37.  URL url = new URL("https://codingconnect.net/feed");
  38.  XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
  39.  factory.setNamespaceAware(false);
  40.  XmlPullParser xpp = factory.newPullParser();
  41.  // We will get the XML from an input stream
  42.  xpp.setInput(getInputStream(url), "UTF_8");
  43.  boolean insideItem = false;
  44.  // Returns the type of current event: START_TAG, END_TAG, etc..
  45.  int eventType = xpp.getEventType();
  46.  while (eventType != XmlPullParser.END_DOCUMENT)
  47.  {
  48.  if (eventType == XmlPullParser.START_TAG)
  49.  {
  50.  if (xpp.getName().equalsIgnoreCase("item"))
  51.  {
  52. insideItem = true;
  53.  }
  54.  else if (xpp.getName().equalsIgnoreCase("title"))
  55.  {
  56.  if (insideItem)
  57.  headlines.add(xpp.nextText()); //extract the headline
  58.  }
  59.  else if (xpp.getName().equalsIgnoreCase("link"))
  60.  {
  61.  if (insideItem)
  62.  links.add(xpp.nextText()); //extract the link of article
  63.  }
  64.  }
  65.  else if(eventType==XmlPullParser.END_TAG &&
  66. xpp.getName().equalsIgnoreCase("item"))
  67.  {
  68.  insideItem=false;
  69.  }
  70.  eventType = xpp.next(); //move to next element
  71.  }
  72.  }
  73.  catch (MalformedURLException e)
  74.  {
  75.  e.printStackTrace();
  76.  }
  77.  catch (XmlPullParserException e)
  78.  {
  79.  e.printStackTrace();
  80.  }
  81.  catch (IOException e)
  82. {
  83. e.printStackTrace();
  84.  }
  85.  return null;
  86.  }
  87.  protected void onPostExecute(ArrayAdapter adapter)
  88.  {
  89.  adapter = new ArrayAdapter(MainActivity.this,
  90. android.R.layout.simple_list_item_1,
  91. headlines);
  92.  setListAdapter(adapter);
  93.  }
  94.  }
  95.  @Override
  96.  protected void onListItemClick(ListView l, View v, int position, long id)
  97.  {
  98.  Uri uri = Uri.parse((links.get(position)).toString());
  99.  Intent intent = new Intent(Intent.ACTION_VIEW, uri);
  100.  startActivity(intent);
  101.  }
  102.  public InputStream getInputStream(URL url)
  103.  {
  104.  try
  105.  {
  106.  return url.openConnection().getInputStream();
  107.  }
  108.  catch (IOException e)
  109.  {
  110.  return null;
  111.  }
  112.  }
  113. }
RAW Paste Data