【问题标题】:Parsing RSS feed to android application将 RSS 提要解析到 android 应用程序
【发布时间】:2011-02-10 17:25:45
【问题描述】:

我正在构建一个狩猎应用程序,我需要来自this 站点的 RSS 提要形式的天气信息。

我使用了来自this 站点的代码,它列出了提要,但是当我单击一个项目时,它并没有将它连接到站点以获取更多信息。我想获取温度和风力信息,但我不知道如何获取,因为我是编程初学者。

我非常感谢任何帮助,尤其是可以解决我的问题的代码形式。

【问题讨论】:

标签: android parsing rss


【解决方案1】:

这是从 URL 获取 RSS Feed 并将其列在 android 的列表视图中的代码。

你需要先创建一个扩展ListActivity的类,然后放上这段代码:

    // Initializing instance variables
    headlines = new ArrayList();
    links = new ArrayList();

    try {
        URL url = new URL("http://www.RSS-Feed-URL-HERE");

        XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
        factory.setNamespaceAware(false);
        XmlPullParser xpp = factory.newPullParser();

            // We will get the XML from an input stream
        xpp.setInput(getInputStream(url), "UTF_8");

            /* We will parse the XML content looking for the "<title>" tag which appears inside the "<item>" tag.
             * However, we should take in consideration that the rss feed name also is enclosed in a "<title>" tag.
             * As we know, every feed begins with these lines: "<channel><title>Feed_Name</title>...."
             * so we should skip the "<title>" tag which is a child of "<channel>" tag,
             * and take in consideration only "<title>" tag which is a child of "<item>"
             *
             * In order to achieve this, we will make use of a boolean variable.
             */
        boolean insideItem = false;

            // Returns the type of current event: START_TAG, END_TAG, etc..
        int eventType = xpp.getEventType();
        while (eventType != XmlPullParser.END_DOCUMENT) {
            if (eventType == XmlPullParser.START_TAG) {

                if (xpp.getName().equalsIgnoreCase("item")) {
                    insideItem = true;
                } else if (xpp.getName().equalsIgnoreCase("title")) {
                    if (insideItem)
                        headlines.add(xpp.nextText()); //extract the headline
                } else if (xpp.getName().equalsIgnoreCase("link")) {
                    if (insideItem)
                        links.add(xpp.nextText()); //extract the link of article
                }
            }else if(eventType==XmlPullParser.END_TAG && xpp.getName().equalsIgnoreCase("item")){
                insideItem=false;
            }

            eventType = xpp.next(); //move to next element
        }

    } catch (MalformedURLException e) {
        e.printStackTrace();
    } catch (XmlPullParserException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

    // Binding data
    ArrayAdapter adapter = new ArrayAdapter(this,
            android.R.layout.simple_list_item_1, headlines);

    setListAdapter(adapter);


}
public InputStream getInputStream(URL url) {
       try {
           return url.openConnection().getInputStream();
       } catch (IOException e) {
           return null;
         }
    }

@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
   Uri uri = Uri.parse((String) links.get(position));
   Intent intent = new Intent(Intent.ACTION_VIEW, uri);
   startActivity(intent);
}

【讨论】:

    【解决方案2】:

    您可以在google play 上下载并查看我的项目。这个项目是关于一些土耳其体育频道的。就像在一个应用程序中一样。

    您可以在github查看项目源代码。

    【讨论】:

      【解决方案3】:

      您可以访问任何流行的天气网站,例如 Weather.com,并查找其 API 部分。大多数网站都提供了用于开发应用程序的 API。 Weather.com 的 API 部分实际上会指导您完成它的实现过程。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2023-04-06
        • 2014-08-26
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-04-05
        • 1970-01-01
        • 2013-03-11
        相关资源
        最近更新 更多