从谷歌警报解析时无法获取所有文本

时间:2015-02-23 16:48:36

标签: android parsing rss atom-feed google-alerts

我已经从Google Alerts Source制作了一个Android RSS Feed应用程序。在我编译和运行之后只显示了大约一半的文章或者无法从一个来源获取所有文本但是没有崩溃。

emulator screenshot

来自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));
    }
} }

来自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();

} }

来自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" />

我如何修复它以便显示?

0 个答案:

没有答案