我正在开发一个完全基于 RSS FEED 的应用程序,我想使用 XML Parser 解析 RSS 链接,我遵循了一些教程并使用 XMLPull Parser 实现代码,但是当我想解析http://feeds.pcworld.com /pcworld/latestnews这个链接,我得到了正确的响应,但是当我想解析http://www.pokerlistings.com/feed/news这个链接时,解析器没有响应我..我不知道为什么?
我花了 1 周时间解决这个问题.. 请帮助我。
这是我的代码
MainActivity.java
package com.rajeshvijayakumar.xmlpullparser;
import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import com.rajeshvijayakumar.model.RSSFeed;
import com.rajeshvijayakumar.parser.NewsFeedParser;
public class MainActivity extends Activity implements OnItemClickListener {
private ListView mRssListView;
private NewsFeedParser mNewsFeeder;
private List<RSSFeed> mRssFeedList;
private RssAdapter mRssAdap;
private static final String TOPSTORIES ="http://www.pokerlistings.com/feed/blogs";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.rss_feed_view);
mRssListView = (ListView) findViewById(R.id.rss_list_view);
mRssFeedList = new ArrayList<RSSFeed>();
new DoRssFeedTask().execute(TOPSTORIES);
mRssListView.setOnItemClickListener(this);
}
private class RssAdapter extends ArrayAdapter<RSSFeed> {
private List<RSSFeed> rssFeedLst;
public RssAdapter(Context context, int textViewResourceId, List<RSSFeed> rssFeedLst) {
super(context, textViewResourceId, rssFeedLst);
this.rssFeedLst = rssFeedLst;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = convertView;
RssHolder rssHolder = null;
if (convertView == null) {
view = View.inflate(MainActivity.this, R.layout.rss_list_item, null);
rssHolder = new RssHolder();
rssHolder.rssTitleView = (TextView) view.findViewById(R.id.rss_title_view);
view.setTag(rssHolder);
} else {
rssHolder = (RssHolder) view.getTag();
}
RSSFeed rssFeed = rssFeedLst.get(position);
rssHolder.rssTitleView.setText(rssFeed.getTitle());
return view;
}
}
static class RssHolder {
public TextView rssTitleView;
}
public class DoRssFeedTask extends AsyncTask<String, Void, List<RSSFeed>> {
ProgressDialog prog;
String jsonStr = null;
Handler innerHandler;
@Override
protected void onPreExecute() {
prog = new ProgressDialog(MainActivity.this);
prog.setMessage("Loading....");
prog.show();
}
@Override
protected List<RSSFeed> doInBackground(String... params) {
for (String urlVal : params) {
mNewsFeeder = new NewsFeedParser(urlVal);
}
mRssFeedList = mNewsFeeder.parse();
return mRssFeedList;
}
@Override
protected void onPostExecute(List<RSSFeed> result) {
prog.dismiss();
runOnUiThread(new Runnable() {
@Override
public void run() {
mRssAdap = new RssAdapter(MainActivity.this, R.layout.rss_list_item,
mRssFeedList);
int count = mRssAdap.getCount();
if (count != 0 && mRssAdap != null) {
mRssListView.setAdapter(mRssAdap);
}
}
});
}
@Override
protected void onProgressUpdate(Void... values) {
}
}
@Override
public void onItemClick(AdapterView<?> adapterView, View v, int position, long id) {
}
}
RSSFeed.java
package com.rajeshvijayakumar.model;
import java.io.Serializable;
import java.util.Date;
public class RSSFeed implements Serializable {
private String title;
private String link;
private String description;
private String category;
private String pubDate;
private String guid;
private String feedburnerOrigLink;
public RSSFeed() {
}
public RSSFeed(String title, String link, String description, String category, String pubDate,
String guid, String feedburnerOrigLink) {
this.title = title;
this.link = link;
this.description = description;
this.category = category;
this.pubDate = pubDate;
this.guid = guid;
this.feedburnerOrigLink = feedburnerOrigLink;
}
public String getTitle() {
return title;
}
public String getLink() {
return link;
}
public String getDescription() {
return description;
}
public String getCategory() {
return category;
}
public String getPubDate() {
return pubDate;
}
public String getGuid() {
return guid;
}
public String getFeedburnerOrigLink() {
return feedburnerOrigLink;
}
public void setTitle(String title) {
this.title = title;
}
public void setLink(String link) {
this.link = link;
}
public void setDescription(String description) {
this.description = description;
}
public void setCategory(String category) {
this.category = category;
}
public void setPubDate(String pubDate) {
this.pubDate = pubDate;
}
public void setGuid(String guid) {
this.guid = guid;
}
public void setFeedburnerOrigLink(String feedburnerOrigLink) {
this.feedburnerOrigLink = feedburnerOrigLink;
}
}
NewsFeedParser.java
package com.rajeshvijayakumar.parser;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserFactory;
import com.rajeshvijayakumar.model.RSSFeed;
public class NewsFeedParser {
private InputStream urlStream;
private XmlPullParserFactory factory;
private XmlPullParser parser;
private List<RSSFeed> rssFeedList;
private RSSFeed rssFeed;
private String urlString;
private String tagName;
private String title;
private String link;
private String description;
private String category;
private String pubDate;
private String guid;
private String feedburner;
public static final String ITEM = "item";
public static final String CHANNEL = "channel";
public static final String TITLE = "title";
public static final String LINK = "link";
public static final String DESCRIPTION = "description";
public static final String CATEGORY = "category";
public static final String PUBLISHEDDATE = "pubDate";
public static final String GUID = "guid";
public static final String FEEDBURNERORIGLINK = "feedburner:origLink";
public NewsFeedParser(String urlString) {
this.urlString = urlString;
}
public static InputStream downloadUrl(String urlString) throws IOException {
URL url = new URL(urlString);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("POST");
conn.setDoInput(true);
conn.connect();
InputStream stream = conn.getInputStream();
return stream;
}
public List<RSSFeed> parse() {
try {
int count = 0;
factory = XmlPullParserFactory.newInstance();
parser = factory.newPullParser();
// urlStream = downloadUrl(urlString);
URL url = new URL("http://www.pokerlistings.com/feed/blogs");
parser.setInput(url.openConnection().getInputStream(), "UTF_8");
// parser.setInput(urlStream, null);
// URL url = new URL("http://www.rssboard.org/files/sample-rss-2.xml");
// XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
// factory.setNamespaceAware(false);
// XmlPullParser xpp = factory.newPullParser();
// xpp.setInput(url.openConnection().getInputStream(), "UTF_8");
int eventType = parser.getEventType();
boolean done = false;
rssFeed = new RSSFeed();
rssFeedList = new ArrayList<RSSFeed>();
while (eventType != XmlPullParser.END_DOCUMENT && !done) {
tagName = parser.getName();
switch (eventType) {
case XmlPullParser.START_DOCUMENT:
break;
case XmlPullParser.START_TAG:
if (tagName.equals(ITEM)) {
rssFeed = new RSSFeed();
}
if (tagName.equals(TITLE)) {
title = parser.nextText().toString();
}
if (tagName.equals(LINK)) {
link = parser.nextText().toString();
}
if (tagName.equals(DESCRIPTION)) {
description = parser.nextText().toString();
}
if (tagName.equals(CATEGORY)) {
category = parser.nextText().toString();
}
if (tagName.equals(PUBLISHEDDATE)) {
pubDate = parser.nextText().toString();
}
if (tagName.equals(GUID)) {
guid = parser.nextText().toString();
}
if (tagName.equals(FEEDBURNERORIGLINK)) {
feedburner = parser.nextText().toString();
}
break;
case XmlPullParser.END_TAG:
System.out.println(">>>>>>> NewsFeedParser END TAG");
if (tagName.equals(CHANNEL)) {
done = true;
} else if (tagName.equals(ITEM)) {
rssFeed = new RSSFeed(title, link, description, category, pubDate,
guid,
feedburner);
rssFeedList.add(rssFeed);
}
break;
}
eventType = parser.nextToken();
//eventType = parser.next();
// eventType++;
System.out.println(">>>>>>> NewsFeedParser parse method event type in ="+eventType);
}
} catch (Exception e) {
e.printStackTrace();
}
return rssFeedList;
}
}
提前致谢