android - 将 RSS 提要解析为 android 应用程序

标签 android parsing rss

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

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

我非常感谢任何帮助,尤其是能够解决我的问题的代码。

最佳答案

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

你需要先创建一个扩展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);
}

关于android - 将 RSS 提要解析为 android 应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4960499/

相关文章:

android - 如何删除 Android 中的画线

javascript - 查找字符串行是否嵌套或者是另一行的子行

css - 更改边框 CSS 的宽度

c# - 我试图从网站上读取 RSS 提要,但希伯来语字体看起来很乱,我该如何修复它?

c - 在没有 strtok/lexer 的情况下将字符串解析为标记

javascript - AngularJS FeedReader - 不填充数组

android - backgroundTint 对 Lollipop 版本没有影响

android - 引用项目中的依赖问题

Android 模拟器在加载屏幕上卡住

java - 如果动态 Web 元素没有任何值,则分配什么值