Android RSS阅读器,无法提取RSS源
下面是我从网站教程中获得的RSSReader,但它是一个旧教程,使用的是旧SDK版本。因此,我修改了它,没有任何错误,但当我运行代码时,我无法获得提要。你能帮我找到问题吗?它只在LogCat中显示“getfeed问题”Android RSS阅读器,无法提取RSS源,android,rss,Android,Rss,下面是我从网站教程中获得的RSSReader,但它是一个旧教程,使用的是旧SDK版本。因此,我修改了它,没有任何错误,但当我运行代码时,我无法获得提要。你能帮我找到问题吗?它只在LogCat中显示“getfeed问题” public class RSSReader extends Activity implements OnItemClickListener{ public final String RSSFEED = "http://www.ibm.com/developerworks/vi
public class RSSReader extends Activity implements OnItemClickListener{
public final String RSSFEED = "http://www.ibm.com/developerworks/views/rss/customrssatom.jsp?zone_by=XML&zone_by=Java&zone_by=Rational&zone_by=Linux&zone_by=Open+source&zone_by=WebSphere&type_by=Tutorials&search_by=&day=1&month=06&year=2007&max_entries=20&feed_by=rss&isGUI=true&Submit.x=48&Submit.y=14";
public final String tag = "RSSReader";
private RSSFeed feed = null;
private Handler handler = new Handler();
private ProgressDialog dialog;
/** Called when the activity is first created. */
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.main);
dialog = ProgressDialog.show(RSSReader.this, "Loading", "Loading, please wait..");
Thread t = new Thread() {
public void run() {
feed = getFeed(RSSFEED);
handler.post(new Runnable() {
public void run() {
dialog.dismiss();
UpdateDisplay();
};
});
}
};
t.start();
}
private RSSFeed getFeed(String urlToRssFeed){
try{
URL url = new URL(urlToRssFeed);
SAXParserFactory factory = SAXParserFactory.newInstance(); // create the factory
SAXParser parser = factory.newSAXParser(); // create a parser
XMLReader xmlreader = parser.getXMLReader(); // create the reader (scanner)
RSSHandler theRssHandler = new RSSHandler(); // instantiate our handler
xmlreader.setContentHandler(theRssHandler); // assign our handler
InputSource is = new InputSource(url.openStream()); // get our data via the url class
xmlreader.parse(is); // perform the synchronous parse
return theRssHandler.getFeed(); // get the results - should be a fully populated RSSFeed instance, or null on error
}
catch (Exception ee){
Log.i(tag, "getfeed problem");
return null;
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
menu.addSubMenu(0, 0, 0, "Choose RSS Feed");
menu.addSubMenu(0, 1, 0, "Refresh");
Log.i(tag, "onCreateOptionsMenu");
return true;
}
public boolean onOptionsItemSelected(MenuItem item){
switch (item.getItemId()) {
case 0:
Log.i(tag,"Set RSS Feed");
return true;
case 1:
Log.i(tag,"Refreshing RSS Feed");
return true;
}
return false;
}
private void UpdateDisplay(){
TextView feedtitle = (TextView) findViewById(R.id.feedtitle);
TextView feedpubdate = (TextView) findViewById(R.id.feedpubdate);
ListView itemlist = (ListView) findViewById(R.id.itemlist);
if (feed == null){
feedtitle.setText("No RSS Feed Available lo..");
return;
}
feedtitle.setText(feed.getTitle());
feedpubdate.setText(feed.getPubDate());
ArrayAdapter<RSSItem> adapter = new ArrayAdapter<RSSItem>(this,android.R.layout.simple_list_item_1,feed.getAllItems());
itemlist.setAdapter(adapter);
itemlist.setOnItemClickListener(this);
itemlist.setSelection(0);
}
public void onItemClick(AdapterView<?> parent, View v, int position, long id){
Log.i(tag,"item clicked! [" + feed.getItem(position).getTitle() + "]");
Intent itemintent = new Intent(this,ShowDescription.class);
Bundle b = new Bundle();
b.putString("title", feed.getItem(position).getTitle());
b.putString("description", feed.getItem(position).getDescription());
b.putString("link", feed.getItem(position).getLink());
b.putString("pubdate", feed.getItem(position).getPubDate());
itemintent.putExtra("android.intent.extra.INTENT", b);
startActivityForResult (itemintent,0);
}
}
public类RSSReader扩展活动实现了mclicklinklistener{
公共最终字符串RSSFEED=”http://www.ibm.com/developerworks/views/rss/customrssatom.jsp?zone_by=XML&zone_by=Java&zone_by=Rational&zone_by=Linux&zone_by=Open+source&zone\u by=WebSphere&type\u by=Tutorials&search\u by=&day=1&month=06&year=2007&max\u entries=20&feed\u by=rss&isGUI=true&Submit.x=48&Submit.y=14”;
公共最终字符串tag=“RSSReader”;
专用RSSFeed feed=null;
私有处理程序=新处理程序();
私人对话;
/**在首次创建活动时调用*/
创建公共空间(捆绑冰柱){
超级冰柱;
setContentView(R.layout.main);
dialog=ProgressDialog.show(RSSReader.this,“正在加载”,“正在加载,请稍候…”);
线程t=新线程(){
公开募捐{
feed=getFeed(RSSFEED);
handler.post(新的Runnable(){
公开募捐{
dialog.dismise();
UpdateDisplay();
};
});
}
};
t、 start();
}
私有RSSFeed getFeed(字符串urlToRssFeed){
试一试{
URL=新URL(urlToRssFeed);
SAXParserFactory=SAXParserFactory.newInstance();//创建工厂
SAXParser parser=factory.newSAXParser();//创建一个解析器
XMLReader=parser.getXMLReader();//创建读取器(扫描仪)
RSSHandler theRssHandler=new RSSHandler();//实例化我们的处理程序
setContentHandler(ThersHandler);//分配我们的处理程序
InputSource is=new InputSource(url.openStream());//通过url类获取数据
parse(is);//执行同步解析
return theRssHandler.getFeed();//获取结果-应该是一个完全填充的RSSFeed实例,或者在出现错误时为null
}
捕获(异常ee){
Log.i(标记“getfeed问题”);
返回null;
}
}
@凌驾
公共布尔onCreateOptions菜单(菜单){
添加子菜单(0,0,0,“选择RSS源”);
添加子菜单(0,1,0,“刷新”);
Log.i(标记“onCreateOptions菜单”);
返回true;
}
公共布尔值onOptionsItemSelected(菜单项项){
开关(item.getItemId()){
案例0:
Log.i(标签,“设置RSS提要”);
返回true;
案例1:
Log.i(标签,“刷新RSS提要”);
返回true;
}
返回false;
}
私有void UpdateDisplay(){
TextView feedtitle=(TextView)findViewById(R.id.feedtitle);
TextView feedpubdate=(TextView)findViewById(R.id.feedpubdate);
ListView itemlist=(ListView)findViewById(R.id.itemlist);
if(feed==null){
setText(“没有可用的RSS提要”);
返回;
}
feedtitle.setText(feed.getTitle());
feedpubdate.setText(feed.getPubDate());
ArrayAdapter=newArrayAdapter(这是android.R.layout.simple_list_item_1,feed.getAllItems());
itemlist.setAdapter(适配器);
itemlist.setOnItemClickListener(此);
itemlist.setSelection(0);
}
public void onItemClick(AdapterView父视图、视图v、整型位置、长id){
Log.i(标记“单击的项”[“+feed.getItem(position.getTitle()+”]);
Intent itemintent=新的Intent(这个,showsdescription.class);
Bundle b=新Bundle();
b、 putString(“title”,feed.getItem(position.getTitle());
b、 putString(“description”,feed.getItem(position.getDescription());
b、 putString(“link”,feed.getItem(position.getLink());
b、 putString(“pubdate”,feed.getItem(position.getPubDate());
itemintent.putExtra(“android.intent.extra.intent”,b);
startActivityForResult(itemintent,0);
}
}
如果您需要完整的项目,我将向您展示所有java文件或上载项目文件。好吧,我也遇到了同样的问题,但我最终解决了它。我假设您有一个RSS解析器,所以这里是活动本身的解决方案。修改此代码:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.feed_layout);
if (haveNetworkConnection()== false) crea_connessione();
itemlist = new ArrayList<RSSItem>();
new RetrieveRSSFeeds().execute();
}
//check internet connection
private void crea_connessione()
{
final AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("No Internet Connection. Activate Now?")
.setCancelable(false)
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(@SuppressWarnings("unused") final DialogInterface dialog, @SuppressWarnings("unused") final int id) {
startActivity(new Intent(Settings.ACTION_WIFI_SETTINGS));
}
})
.setNegativeButton("No", new DialogInterface.OnClickListener() {
public void onClick(final DialogInterface dialog, @SuppressWarnings("unused") final int id) {
dialog.cancel();
finish();
}
});
final AlertDialog alert = builder.create();
alert.show();
}
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
RSSItem data = itemlist.get(position);
Intent intent = new Intent(Intent.ACTION_VIEW,Uri.parse(data.link));
startActivity(intent);
}
private void retrieveRSSFeed(String urlToRssFeed,ArrayList<RSSItem> list)
{
try
{
URL url = new URL(urlToRssFeed);
SAXParserFactory factory = SAXParserFactory.newInstance();
SAXParser parser = factory.newSAXParser();
XMLReader xmlreader = parser.getXMLReader();
RSSParser theRssHandler = new RSSParser(list);
xmlreader.setContentHandler(theRssHandler);
InputSource is = new InputSource(url.openStream());
xmlreader.parse(is);
}
catch (Exception e)
{
e.printStackTrace();
}
}
private class RetrieveRSSFeeds extends AsyncTask<Void, Void, Void>
{
private ProgressDialog progress = null;
@Override
protected Void doInBackground(Void... params) {
retrieveRSSFeed("http://blog.qubiz.com/index.php/feed",itemlist);
rssadaptor = new RSSListAdaptor(RSSListActivity.this, R.layout.rssitemview,itemlist);
return null;
}
@Override
protected void onCancelled() {
super.onCancelled();
}
@Override
protected void onPreExecute() {
progress = ProgressDialog.show(
RSSListActivity.this, null, "Loading RSS Feed... Please wait");
super.onPreExecute();
}
@Override
protected void onPostExecute(Void result) {
setListAdapter(rssadaptor);
progress.dismiss();
super.onPostExecute(result);
}
@Override
protected void onProgressUpdate(Void... values) {
super.onProgressUpdate(values);
}
}
private class RSSListAdaptor extends ArrayAdapter<RSSItem>{
private List<RSSItem> objects = null;
public RSSListAdaptor(Context context, int textviewid, List<RSSItem> objects) {
super(context, textviewid, objects);
this.objects = objects;
}
@Override
public int getCount() {
return ((null != objects) ? objects.size() : 0);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public RSSItem getItem(int position) {
return ((null != objects) ? objects.get(position) : null);
}
public View getView(int position, View convertView, ViewGroup parent) {
View view = convertView;
if(null == view)
{
LayoutInflater vi = (LayoutInflater)RSSListActivity.this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = vi.inflate(R.layout.rssitemview, null);
}
RSSItem data = objects.get(position);
if(null != data)
{
TextView title = (TextView)view.findViewById(R.id.txtTitle);
TextView date = (TextView)view.findViewById(R.id.txtDate);
TextView description = (TextView)view.findViewById(R.id.txtDescription);
title.setText(data.title);
date.setText("on " + data.date);
String prova = android.text.Html.fromHtml(data.description).toString();
//description.setText(data.description);
description.setText(prova);
}
return view;
}
}
@覆盖
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
requestWindowFeature(窗口。功能\u无\u标题);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_全屏,
WindowManager.LayoutParams.FLAG(全屏);
setContentView(R.layout.feed_布局);
如果(haveNetworkConnection()==false)创建连接();
itemlist=新的ArrayList();
新的RetrieversFeeds().execute();
}
//检查互联网连接
私人无效信用证()
{
final AlertDialog.Builder=新建AlertDialog.Builder(此);
builder.setMessage(“没有Internet连接。是否立即激活?”)
.setCancelable(错误)
.setPositiveButton(“是”,新的DialogInterface.OnClickListener(){
public void onClick(@SuppressWarnings(“未使用”)最终对话框接口对话框,@SuppressWarnings(“未使用”)最终int-id){
startActivity(新意图(设置、动作和设置));
}
})
.setNegativeButton(“否”,新的DialogInterface.OnClickListener(){
public void onClick(final DialogInterface dialog,@SuppressWarnings(“未使用”)final int-id){
dialog.cancel();
完成();
}
});
final AlertDialog alert=builder.create();
alert.show();
}
@凌驾
受保护的void onListItemClick(列表视图l、视图v、整数位置、长id){
super.onListItemClick(左、右、位置、id);