2011-12-03 165 views
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文件或上傳項目文件。

+0

您應該附加調試器並查看異常。 – schlingel

+0

對不起,你能教我如何?因爲我是Android和eclipse的新手。 – ooivanoo

+0

這是我的項目文件:http://www.sendspace.com/file/848qep – ooivanoo

回答

1

那麼,我有同樣的問題,但我最終解決了它。我假設你有一個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; 
     } 
    } 

我希望這對你有所幫助。