我已经从Google Alerts Source制作了一个 Android RSS Feed 应用程序。在我编译并运行后仅显示大约一半的文章或无法从一个来源获取所有文本但不会崩溃。
来自 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" />
我怎样才能修复它以便它会显示?