Android Open Source - RssReader Get Rss Items






From Project

Back to project page RssReader.

License

The source code is released under:

MIT License

If you think the Android project RssReader listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.vicmns.rssreader.http;
/*from w  w w  .j  a v a 2 s .  co m*/
import java.io.StringReader;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import java.util.Locale;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserFactory;

import android.content.Context;
import android.util.Log;

import com.android.volley.RequestQueue;
import com.android.volley.Response.ErrorListener;
import com.android.volley.Response.Listener;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import com.vicmns.rssreader.interfaces.GetRssItemsCallbacks;
import com.vicmns.rssreader.models.RssItem;

public class GetRssItems {
  private RequestQueue queue;
  private Context context;
  private GetRssItemsCallbacks rssItemsCallbacks;
  private Calendar cal = Calendar.getInstance();

  public GetRssItems(Context context, GetRssItemsCallbacks rssItemsCallbacks) {
    this.context = context;
    queue = Volley.newRequestQueue(context);
    this.rssItemsCallbacks = rssItemsCallbacks;
  }

  public void getNewRssItems() {
    StringRequest rssStringItems = new StringRequest(
        "http://www.engadget.com/rss.xml", new Listener<String>() {
          @Override
          public void onResponse(String response) {
            try {
              Log.i(this.getClass().getSimpleName(), "Service ok!...");
              rssItemsCallbacks.onItemsDownloaded(parseStringResponse(response));
            } catch (Exception e) {
              e.printStackTrace();
            }
          }
        }, new ErrorListener() {
          @Override
          public void onErrorResponse(VolleyError error) {
            Log.i(this.getClass().getSimpleName(), "Service error!...");
            rssItemsCallbacks.onConnectionError();
          }
        });
    //rssStringItems.setTag(this);
    Log.i(this.getClass().getSimpleName(), "Downloading feed!...");
    queue.add(rssStringItems);
//    AssetManager assetManager = context.getAssets();
//    InputStream ims;
//    try {
//      ims = assetManager.open("testRss.xml");
//      rssItemsCallbacks.onItemsDownloaded(parseStringResponse(ims));
//    } catch (Exception e) {
//      // TODO Auto-generated catch block
//      e.printStackTrace();
//    }

  }
  
  public void cancelWSCall() {
    queue.cancelAll(this);
  }

  private List<RssItem> parseStringResponse(String response)
      throws Exception {
    List<RssItem> rssItemList = new ArrayList<RssItem>();
    XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
    XmlPullParser parser = factory.newPullParser();
    String tagName;
    //parser.setInput(inputStream, "UTF_8");
    parser.setInput(new StringReader(response));
    int eventType = parser.getEventType();
    while (eventType != XmlPullParser.END_DOCUMENT) {
      tagName = parser.getName();
      switch (eventType) {
      case XmlPullParser.START_DOCUMENT:
        break;
      case XmlPullParser.START_TAG:
        if (tagName.equals("item")) {
          rssItemList.add(parseXMLItem(parser));
        }  else if (tagName.equals("pubDate")) {
          // Mon, 14 Apr 2014 11:00:00 -0400
          SimpleDateFormat sdf = new SimpleDateFormat(
              "EEE, d MMM yyyy HH:mm:ss Z", Locale.ENGLISH);
          cal.setTime(sdf.parse(parser.nextText().toString()));// all
                                      // done
          rssItemList.get(rssItemList.size() - 1).setPubDate(cal.getTime());
        }
        break;
      case XmlPullParser.END_TAG:
        break;
      }
      eventType = parser.next();
    }

    return rssItemList;
  }

  private RssItem parseXMLItem(XmlPullParser parser) throws Exception {
    RssItem rssItem = new RssItem();
    String tagName;
    int eventType = parser.getEventType();
    Document doc;
    String itemDescription;
    while (eventType != XmlPullParser.END_TAG) {
      tagName = parser.getName();
      switch (eventType) {
      case XmlPullParser.START_TAG:
        if (tagName.equals("title")) {
          rssItem.setTitle(parser.nextText().toString());
        } else if (tagName.equals("link")) {
          rssItem.setLink(parser.nextText().toString());
        } else if (tagName.equals("description")) {
          itemDescription = stripCDATA(parser.nextText());
          doc = Jsoup.parse(itemDescription);
          Element image = doc.select("img").first();
          if(image != null)
            rssItem.setImageURL(image.absUrl("src"));
          itemDescription = itemDescription.replaceAll("<img(.|\n)*?>","");
          rssItem.setDescription(itemDescription.trim());
        } else if (tagName.equals("dc:creator")) {

        }
        break;
      }
      eventType = parser.next();
    }

    return rssItem;
  }
  
  private static String stripCDATA(String s) {
      s = s.trim();
      if (s.startsWith("<![CDATA[")) {
        s = s.substring(9);
        int i = s.indexOf("]]&gt;");
        if (i == -1) {
          throw new IllegalStateException(
              "argument starts with <![CDATA[ but cannot find pairing ]]&gt;");
        }
        s = s.substring(0, i);
      }
      return s;
    }
}




Java Source Code List

com.vicmns.rssreader.activities.MainActivity.java
com.vicmns.rssreader.activities.RssItemDetailsActivity.java
com.vicmns.rssreader.adapters.RssItemsAdapter.java
com.vicmns.rssreader.app.RssReaderApplication.java
com.vicmns.rssreader.http.GetRssItems.java
com.vicmns.rssreader.http.GetWidgetRssItems.java
com.vicmns.rssreader.http.SimpleXmlRequest.java
com.vicmns.rssreader.interfaces.GetRssItemsCallbacks.java
com.vicmns.rssreader.interfaces.HttpConsumerCallbacks.java
com.vicmns.rssreader.interfaces.ListOverlayLayoutCallbacks.java
com.vicmns.rssreader.models.RssItem.java
com.vicmns.rssreader.models.RssItems.java
com.vicmns.rssreader.services.GetRssItemsService.java
com.vicmns.rssreader.views.ListOverlayLayoutView.java
com.vicmns.rssreader.views.SquareImageView.java
com.vicmns.rssreader.widget.WidgetListProvider.java
com.vicmns.rssreader.widget.WidgetProvider.java
com.vicmns.rssreader.widget.WidgetService.java
fr.castorflex.android.smoothprogressbar.ColorsShape.java
fr.castorflex.android.smoothprogressbar.ContentLoadingSmoothProgressBar.java
fr.castorflex.android.smoothprogressbar.SmoothProgressBarUtils.java
fr.castorflex.android.smoothprogressbar.SmoothProgressBar.java
fr.castorflex.android.smoothprogressbar.SmoothProgressDrawable.java
org.robolectric.shadows.ShadowSupportMenuInflater.java