Hy,我正在尝试显示我的RSS提要,但问题是,在尝试显示标题时,rss feed以某种方式显示所有内容(链接,标题甚至是我正在尝试阅读的页面的名称)来自)。 这是代码示例。
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
List<JSONObject> jobs = new ArrayList<JSONObject>();
try {
} catch (Exception e) {
Log.e("RSS ERROR", "Error loading RSS Feed Stream >> " + e.getMessage() + " //" + e.toString());
}
final Button button1 = (Button) findViewById(R.id.button1);
button1.setText(aaa);
}
public static List<JSONObject> getLatestRssFeed(){
String feed = "http://www.net.hr/rss/feeds/naslovnica/index.xml";
RSSHandler rh = new RSSHandler();
List<Article> articles = rh.getLatestArticles(feed);
Log.e("RSS ERROR", "Number of articles " + articles.size());
return fillData(articles);
}
private static List<JSONObject> fillData(List<Article> articles) {
List<JSONObject> items = new ArrayList<JSONObject>();
for (Article article : articles) {
JSONObject current = new JSONObject();
try {
buildJsonObject(article, current);
aaa=article.getTitle();
/*
This is where I'm trying to get the title from actually
*/
} catch (JSONException e) {
Log.e("RSS ERROR", "Error creating JSON Object from RSS feed");
}
items.add(current);
}
return items;
}
private static void buildJsonObject(Article article, JSONObject current) throws JSONException {
String title = article.getTitle();
}
还有这个课程
public class RSSHandler extends DefaultHandler {
private Article currentArticle = new Article();
private List<Article> articleList = new ArrayList<Article>();
private int articlesAdded = 0;
private static final int ARTICLES_LIMIT = 1;
StringBuffer chars = new StringBuffer();
public void startElement(String uri, String localName, String qName, Attributes atts) {
chars = new StringBuffer();
}
public void endElement(String uri, String localName, String qName) throws SAXException {
if (localName.equalsIgnoreCase("title"))
{
Log.d("LOGGING RSS XML", "Setting article title: " + chars.toString());
currentArticle.setTitle(chars.toString());
}
else if (localName.equalsIgnoreCase("description"))
{
Log.d("LOGGING RSS XML", "Setting article description: " + chars.toString());
currentArticle.setDescription(chars.toString());
}
else if (localName.equalsIgnoreCase("pubDate"))
{
Log.d("LOGGING RSS XML", "Setting article published date: " + chars.toString());
currentArticle.setPubDate(chars.toString());
}
else if (localName.equalsIgnoreCase("encoded"))
{
Log.d("LOGGING RSS XML", "Setting article content: " + chars.toString());
currentArticle.setEncodedContent(chars.toString());
}
else if (localName.equalsIgnoreCase("item"))
{
}
else if (localName.equalsIgnoreCase("link"))
{
try {
Log.d("LOGGING RSS XML", "Setting article link url: " + chars.toString());
currentArticle.setUrl(new URL(chars.toString()));
} catch (MalformedURLException e) {
Log.e("RSA Error", e.getMessage());
}
}
if (localName.equalsIgnoreCase("item")) {
articleList.add(currentArticle);
currentArticle = new Article();
articlesAdded++;
if (articlesAdded >= ARTICLES_LIMIT)
{
throw new SAXException();
}
}
}
public void characters(char ch[], int start, int length) {
chars.append(new String(ch, start, length));
}
public List<Article> getLatestArticles(String feedUrl) {
URL url = null;
try {
SAXParserFactory spf = SAXParserFactory.newInstance();
SAXParser sp = spf.newSAXParser();
XMLReader xr = sp.getXMLReader();
url = new URL(feedUrl);
xr.setContentHandler(this);
xr.parse(new InputSource(url.openStream()));
} catch (IOException e) {
Log.e("RSS Handler IO", e.getMessage() + " >> " + e.toString());
} catch (SAXException e) {
Log.e("RSS Handler SAX", e.toString());
} catch (ParserConfigurationException e) {
Log.e("RSS Handler Parser Config", e.toString());
}
return articleList;
}
}
另外请注意,因为这是我第一次在编程时遇到rss,这不是我的代码,而是这里的链接 https://github.com/robhinds/AndroidRssReader/blob/master/src/com/tmm/android/rssreader/RssListAdapter.java 所以我想知道是否还有其他一些问题(比如不同的rss版本或其他东西),但我认为不会导致他的链接对我的应用程序做同样的事情。
无论如何,我需要该应用才能获得12条最新新闻的标题和新闻链接。 感谢