0
以下是我從網站教程中獲得的RSSReader,但它是一箇舊教程,並且使用舊的SDK版本。所以,我修改它沒有任何錯誤,但是當我運行代碼時,我無法獲得一個feed。你能幫我找到問題嗎?它僅在LogCat中顯示「getfeed問題」。Android RSS閱讀器,未能提供RSS訂閱
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);
}
}
如果您需要完整的項目,我會向您展示所有的java文件或上傳項目文件。
您應該附加調試器並查看異常。 – schlingel
對不起,你能教我如何?因爲我是Android和eclipse的新手。 – ooivanoo
這是我的項目文件:http://www.sendspace.com/file/848qep – ooivanoo