实际上在我的应用程序中我想做rss解析。但是当我解析数据时没有给我回复。 我的应用程序中的rss解析器有一些问题。 我已经把我所有的代码和我的rss数据。 我尝试了很多次,但它不会给我任何数据
我的RSS频道数据
<?xml version="1.0" encoding="utf-8"?>
<?xml-stylesheet type="text/css" href="/css/ticketekCore.css" ?>
<?xml-stylesheet type="text/css" href="/css/ticketekPageLayouts.css" ?>
<rss version="2.0">
<channel>
<title>Ticketek Event Listing</title>
<link>http://www.ticketek.com.au/</link>
<description>Ticketek Event Listing</description>
<language>en-au</language>
<item>
<title><![CDATA[The 2016 Cranston Cup Theatresports Grand Final]]></title>
<link>http://premier.ticketek.com.au/shows/show.aspx?sh=TSCRANST16</link>
<guid isPermaLink="true">http://premier.ticketek.com.au/shows/show.aspx?sh=TSCRANST16</guid>
<description><![CDATA[
<div class=" clearfix">
<div class="resultContainer">
<div class="contentImage">
<a href="/shows/show.aspx?sh=TSCRANST16"><img src="//d35kvm5iuwjt9t.cloudfront.net/dbimages/sfx161301.jpg" style="width:61px;height:61px;" alt="The 2016 Cranston Cup Theatresports Grand Final" /></a>
</div>
<div class="contentEvent">
<h6>
<a href="/shows/show.aspx?sh=TSCRANST16">The 2016 Cranston Cup Theatresports Grand Final</a>
</h6>
</div>
<div class="contentEventAndDate clearfix">
<div class="contentLocation">
<strong>Enmore Theatre</strong><br />
NSW/ACT
</div>
<div class="contentDate"> Sun 27 Nov 2016 </div>
<div class="resultBuyNow">
<a class="yellowGradientButton" href="/shows/show.aspx?sh=TSCRANST16&v=NMO">Get Tickets</a>
</div>
</div>
</div>
</div>
]]></description>
</item>
My java code:
Fragment.java
private class MyTask extends AsyncTask<Void, Void, Void>{
@Override
protected void onPreExecute(){
pDialog = (RelativeLayout) ll.findViewById(R.id.progressBarHolder);
}
@Override
protected Void doInBackground(Void... arg0) {
try {
String weburl = RssFragment.this.getArguments().getString(MainActivity.DATA);
URL rssUrl = new URL(weburl);
SAXParserFactory mySAXParserFactory = SAXParserFactory.newInstance();
SAXParser mySAXParser = mySAXParserFactory.newSAXParser();
XMLReader myXMLReader = mySAXParser.getXMLReader();
RSSHandler myRSSHandler = new RSSHandler();
myXMLReader.setContentHandler(myRSSHandler);
InputSource myInputSource = new InputSource(rssUrl.openStream());
myXMLReader.parse(myInputSource);
myRssFeed = myRSSHandler.getFeed();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (ParserConfigurationException e) {
e.printStackTrace();
} catch (SAXException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Void result) {
ListView listview = (ListView) ll.findViewById(R.id.list);
if (myRssFeed != null) {
int size = myRssFeed.getList().size();
Log.d("listsize", String.valueOf(size));
MyCustomAdapter adapter = new MyCustomAdapter(mAct, R.layout.fragment_rss_row, myRssFeed.getList());
listview.setAdapter(adapter);
listview.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View v,
int position, long id) {
Intent intent = new Intent(mAct,
RssDetailActivity.class);
Bundle bundle = new Bundle();
bundle.putString("keyTitle", myRssFeed.getItem(position).getTitle());
bundle.putString("keyDescription", myRssFeed.getItem(position).getDescription());
bundle.putString("keyLink", myRssFeed.getItem(position).getLink());
bundle.putString("keyPubdate", myRssFeed.getItem(position).getPubdate());
bundle.putString("keyThumbnail", myRssFeed.getItem(position).getThumburl());
bundle.putString("keyVideo", myRssFeed.getItem(position).getVideourl());
bundle.putString("keyAudio", myRssFeed.getItem(position).getAudiourl());
intent.putExtras(bundle);
startActivity(intent);
}
});
} else {
String url = RssFragment.this.getArguments().getString(MainActivity.DATA);
String message = null;
if (!url.startsWith("http"))
message = "Debug info: '" + url + "' is most likely not a valid RSS url. Make sure the url entered in your configuration starts with 'http' and verify if it's valid XML using https://validator.w3.org/feed/";
Helper.noConnection(mAct, true, message);
}
if (pDialog.getVisibility() == View.VISIBLE) {
pDialog.setVisibility(View.GONE);
//feedListView.setVisibility(View.VISIBLE);
Helper.revealView(listview,ll);
}
super.onPostExecute(result);
}
}
my RSShandler class
package com.sherdle.universal.rss;
import org.jsoup.Jsoup;
import org.jsoup.select.Elements;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
import android.text.Html;
/**
* This class is used to help us get the right information from the data from the feed.
*/
public class RSSHandler extends DefaultHandler {
private State currentState = State.unknown;
private RSSFeed feed;
private RSSItem item;
private boolean itemFound = false;
private StringBuilder tagContent;
public RSSHandler() {
}
@Override
public void startDocument() throws SAXException {
feed = new RSSFeed();
item = new RSSItem();
}
@Override
public void startElement(final String uri, final String localName, final String qName, final Attributes attributes)
throws SAXException {
currentState = State.unknown;
tagContent = new StringBuilder();
if (localName.equalsIgnoreCase("item") || localName.equalsIgnoreCase("entry")) {
itemFound = true;
item = new RSSItem();
currentState = State.unknown;
} else if (qName.equalsIgnoreCase("title")) {
currentState = State.title;
} else if (qName.equalsIgnoreCase("description") || qName.equalsIgnoreCase("content:encoded")) {
currentState = State.description;
} else if (qName.equalsIgnoreCase("link") || qName.equalsIgnoreCase("origLink")) {
currentState = State.link;
} else if (qName.equalsIgnoreCase("pubdate") || qName.equalsIgnoreCase("published")) {
currentState = State.pubdate;
} else if (qName.equalsIgnoreCase("media:thumbnail")) {
currentState = State.media;
String attrValue = attributes.getValue("url");
item.setThumburl(attrValue);
} else if (qName.equalsIgnoreCase("media:content")){
currentState = State.media;
String attrValue = attributes.getValue("url");
if (attributes.getValue("type") == null || attributes == null){
return;
} else if (attributes.getValue("type").startsWith("image")){
item.setThumburl(attrValue);
} else if (attributes.getValue("type").startsWith("video")){
item.setVideourl(attrValue);
} else if (attributes.getValue("type").startsWith("audio")){
item.setAudiourl(attrValue);
}
} else if (qName.equalsIgnoreCase("enclosure")){
currentState = State.media;
String attrValue = attributes.getValue("url");
if (attributes.getValue("type").startsWith("image")){
item.setThumburl(attrValue);
} else if (attributes.getValue("type").startsWith("video")){
item.setVideourl(attrValue);
} else if (attributes.getValue("type").startsWith("audio")){
item.setAudiourl(attrValue);
}
}
}
@Override
public void endElement(final String uri, final String localName,
final String qName) throws SAXException {
if (localName.equalsIgnoreCase("item") || localName.equalsIgnoreCase("entry")) {
feed.addItem(item);
}
if (itemFound == true) {
// "item" tag found, it's item's parameter
switch (currentState) {
case title:
String title = Html.fromHtml(tagContent.toString().trim()).toString();
item.setTitle(Html.fromHtml(tagContent.toString().trim()).toString());
break;
case description:
String description = tagContent.toString();
item.setDescription(tagContent.toString());
//if thumburl not already set
if (item.getThumburl() == null || item.getThumburl() == ""){
String html = tagContent.toString();
org.jsoup.nodes.Document docHtml = Jsoup
.parse(html);
Elements imgEle = docHtml.select("img");
String source = imgEle.attr("src");
item.setThumburl(source);
}
break;
case link:
String link =tagContent.toString();
item.setLink(tagContent.toString());
break;
case pubdate:
String pubdate =tagContent.toString();
item.setPubdate(tagContent.toString());
break;
case media:
break;
default:
break;
}
} else {
// not "item" tag found, it's feed's parameter
switch (currentState) {
case title:
feed.setTitle(tagContent.toString());
break;
case description:
feed.setDescription(tagContent.toString());
break;
case link:
feed.setLink(tagContent.toString());
break;
case pubdate:
feed.setPubdate(tagContent.toString());
break;
default:
break;
}
}
}
@Override
public void characters(final char[] ch, final int start, final int length)
throws SAXException {
tagContent.append(ch, start, length);
}
public RSSFeed getFeed() {
return feed;
}
public enum State {
unknown, title, description, link, pubdate, media
}
}