Android上的RSS解析 [英] Parsing RSS on Android

查看:141
本文介绍了Android上的RSS解析的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一对夫妇的RSS订阅我需要解析为我的应用程序,我在这里遵循的优秀教程:<一href=\"http://w2davids.word$p$pss.com/android-rssatom-feeds-parsing-with-rome/\">http://w2davids.word$p$pss.com/android-rssatom-feeds-parsing-with-rome/.我修改了样品一点,并得到它做我的需要。所以,我去将它融入我的应用程序,始终得到了强制退出每次我试图显示帖子列表的时间。我修改了code版本如下。它似乎总是连接适配器到ListView时失败,所以我想我的适配器设置错误。什么是奇怪的我们,在调试器,我可以看看适配器,看看我试图获取数据。

在此先感谢:

 公共类视图1扩展活动
{
    / **当第一次创建活动调用。 * /
    私人最终的ArrayList&LT;串GT;名单=新的ArrayList&LT;串GT;();
    私人的ListView ListView的;
    私人ArrayAdapter&LT;串GT;适配器= NULL;
    私人SyndContent说明;
    私人的ArrayList&LT;串GT; D =新的ArrayList&LT;串GT;();    @覆盖
    公共无效的onCreate(捆绑savedInstanceState)
        {
            super.onCreate(savedInstanceState);
            的setContentView(R.layout.main);
            适配器=新ArrayAdapter&LT;串GT;(这一点,R.layout.dataview,R.id.ListItemView);
            getRSS(http://www.example.com/wp/?feed=gig$p$pss&artist=1);
            listView.setAdapter(适配器);
            ListView控件=(ListView控件)this.findViewById(R.id.ListView);            listView.setOnItemClickListener(新OnItemClickListener()
                {
                    @覆盖
                    公共无效onItemClick(适配器视图&LT;&GT;母公司,观景,INT位置,持续时间长)
                        {
                            意向意图=新的Intent();
                            intent.setClassName(com.example.idtb,com.example.idtb.ViewRssDescription);
                            intent.putExtra(DESC,d.get(位置));
                            startActivity(意向);
                        }
                });        }    私人无效getRSS(字符串RSS)
        {            URL feedUrl;
            尝试
                {
                    Log.d(调试,输入+ RSS);
                    feedUrl =新的URL(RSS);                    SyndFeedInput输入=新SyndFeedInput();
                    SyndFeed饲料= input.build(新的XmlReader(feedUrl));
                    列表条目= feed.getEntries();                    迭代器迭代器= entries.listIterator();
                    而(iterator.hasNext())
                        {
                            SyndEntry ENT =(SyndEntry)iterator.next();
                            字符串title = ent.getTitle();
                            DESC = ent.getDescription();
                            d.add(desc.getValue());
                            adapter.add(职称);
                        }
                    adapter.notifyDataSetChanged();                }
            赶上(MalformedURLException的E)
                {
                    e.printStackTrace();
                }
            赶上(抛出:IllegalArgumentException五)
                {
                    e.printStackTrace();
                }
            赶上(FeedException E)
                {
                    e.printStackTrace();
                }
            赶上(IOException异常E)
                {
                    e.printStackTrace();
                }
        }
}


解决方案

我觉得适配器应是这个样子:

 最后ArrayAdapter&LT;串GT;适配器=新ArrayAdapter&LT;串GT;(这一点,android.R.layout.simple_list_item_1,LST);

I've got a couple RSS feeds I need to parse for my app and I followed the excellent tutorial here: http://w2davids.wordpress.com/android-rssatom-feeds-parsing-with-rome/. I modified the sample a bit and got it to do what I needed. So, I went to integrate it into my app and consistently get a Force Quit every time I try to display the list of posts. My modified version of the code is below. It always seems to fail when attaching the adapter to the ListView, so I assume my adapter is setup incorrectly. What's strange us that in the debugger, I can look at the adapter and see the data I'm trying to fetch.

Thanks in advance:

public class View1 extends Activity
{
    /** Called when the activity is first created. */
    private final ArrayList<String> list = new ArrayList<String>();
    private ListView listView;
    private ArrayAdapter<String> adapter = null;
    private SyndContent desc;
    private ArrayList<String> d = new ArrayList<String>() ;

    @Override
    public void onCreate(Bundle savedInstanceState)
        {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
            adapter = new ArrayAdapter<String>(this, R.layout.dataview, R.id.ListItemView);             
            getRSS("http://www.example.com/wp/?feed=gigpress&artist=1");
            listView.setAdapter(adapter);


            listView = (ListView) this.findViewById(R.id.ListView);

            listView.setOnItemClickListener(new OnItemClickListener()
                {
                    @Override
                    public void onItemClick(AdapterView<?> parent, View view, int position, long duration)
                        {                               
                            Intent intent = new Intent();
                            intent.setClassName("com.example.idtb", "com.example.idtb.ViewRssDescription");
                            intent.putExtra("desc", d.get(position));
                            startActivity(intent);
                        }
                });             

        }

    private void getRSS(String rss)
        {

            URL feedUrl;
            try
                {
                    Log.d("DEBUG", "Entered:" + rss);
                    feedUrl = new URL(rss);

                    SyndFeedInput input = new SyndFeedInput();
                    SyndFeed feed = input.build(new XmlReader(feedUrl));
                    List entries = feed.getEntries();

                    Iterator iterator = entries.listIterator();
                    while (iterator.hasNext())
                        {
                            SyndEntry ent = (SyndEntry) iterator.next();
                            String title = ent.getTitle();
                            desc = ent.getDescription();
                            d.add(desc.getValue());
                            adapter.add(title);
                        }
                    adapter.notifyDataSetChanged();

                }
            catch (MalformedURLException e)
                {
                    e.printStackTrace();
                }
            catch (IllegalArgumentException e)
                {
                    e.printStackTrace();
                }
            catch (FeedException e)
                {
                    e.printStackTrace();
                }
            catch (IOException e)
                {
                    e.printStackTrace();
                }
        }
}

解决方案

I think adapter should look something like this:

final ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, lst); 

这篇关于Android上的RSS解析的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆