Quantcast
Channel: Active questions tagged atom-feed+rss+parsing - Stack Overflow
Viewing all articles
Browse latest Browse all 13

Can't fetch all text when parse from google alerts

0
0

I already made an Android RSS Feed App from Google Alerts Source. After I compiled and running only showing about half article or can't fetch all text from one source but not crash.

emulator screenshot

From RssAtomParseHandler.java

import java.util.ArrayList;
import java.util.List;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

import com.a.b.c.d.e.RssAtomItem;

public class RssAtomParseHandler extends DefaultHandler {

private List<RssAtomItem> rssItems;

// Used to reference item while parsing
private RssAtomItem currentItem;

// Parsing title indicator
private boolean parsingTitle;
// Parsing link indicator
private boolean parsingContents;
// A buffer for title contents
private StringBuffer currentTitleSb;
// A buffer for content tag contents
private StringBuffer currentContentSb;

public RssAtomParseHandler() {
    rssItems = new ArrayList<RssAtomItem>();
}

public List<RssAtomItem> getItems() {
    return rssItems;
}

@Override
public void startElement(String uri, String localName, String qName,   Attributes attributes) throws SAXException {

    if ("entry".equals(qName)) {
        currentItem = new RssAtomItem();
    } else if ("title".equals(qName)) {
        parsingTitle = true;
        currentTitleSb = new StringBuffer();
    } else if ("content".equals(qName)) {
        parsingContents = true;
        currentContentSb = new StringBuffer();
    }
}

@Override
public void endElement(String uri, String localName, String qName) throws SAXException {

    if ("entry".equals(qName)) {
        rssItems.add(currentItem);
        currentItem = null;
    } else if ("title".equals(qName)) {
        parsingTitle = false;

        if (currentItem != null) // There is a title tag for a whole channel present. It is being parsed before the entry tag is present, so we need to check if item is not null
            currentItem.setTitle(currentTitleSb.toString());

    } else if ("content".equals(qName)) {
        parsingContents = false;

        if (currentItem != null)
            currentItem.setContent(currentContentSb.toString());

    }
}

@Override
public void characters(char[] ch, int start, int length) throws SAXException {

    if (parsingTitle) {
        if (currentItem != null)
            currentTitleSb.append(new String(ch, start, length));
    } else if (parsingContents) {
        if (currentItem != null)
            currentContentSb.append(new String(ch, start, length));
    }
} }

From RssAtomReader.java

import java.util.List;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import com.a.b.c.d.e.RssAtomItem;


public class RssAtomReader {

private String rssUrl;

/**
 * Constructor
 * 
 * @param rssUrl
 */
public RssAtomReader(String rssUrl) {
    this.rssUrl = rssUrl;
}

/**
 * Get RSS items.
 * 
 * @return
 */
public List<RssAtomItem> getItems() throws Exception {
    // SAX parse RSS data
    SAXParserFactory factory = SAXParserFactory.newInstance();

    SAXParser saxParser = factory.newSAXParser();

    RssAtomParseHandler handler = new RssAtomParseHandler();

    saxParser.parse(rssUrl, handler);

    return handler.getItems();

} }

From activity_details.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<TextView
    android:id="@+id/detailsTextView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="[POST TITLE GOES HERE]"
    android:textAppearance="?android:attr/textAppearanceMedium" />

<WebView
    android:id="@+id/detailsWebView"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

How I can fix it so it will show ?


Viewing all articles
Browse latest Browse all 13

Latest Images

Trending Articles





Latest Images