2011-12-03 18 views
0

以下はRSSチュートリアルのRSSリーダーですが、古いチュートリアルで古いSDKバージョンを使用しています。だから、私は間違いなくそれを修正しましたが、私がコードを実行すると私はフィードを得ることができません。問題を見つけるのを助けてくれますか? 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; 
     } 
    } 

私はこれが役に立ちました。

関連する問題