簡體   English   中英

Android RSS閱讀器,無法拉入RSS提要

[英]Android RSS Reader, failed to pull rss feed

以下是我從網站教程中獲得的RSSReader,但這是一個舊教程,並且使用的是舊版SDK。 因此,我對其進行了修改,沒有任何錯誤,但是當我運行代碼時,我無法獲得提要。 你能幫我找到問題嗎? 它僅在LogCat中顯示“ getfeed問題”。

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文件或上傳項目文件。

好吧,我遇到了同樣的問題,但最終我解決了。 我假設您有一個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;
        }
    }

希望這對您有所幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM