将RSS feed解析为android应用

将RSS feed解析为android应用,第1张

概述我正在构建一个狩猎应用程序,我需要从this站点以RSS提要的形式获取天气信息.我使用了来自this网站的代码,它列出了提要,但是当我单击某个项目时,它不会将其连接到该网站以获取更多信息.我想获取有关温度和风的信息,但是我不知道如何做,因为我是编程的初学者.我将非常感谢您提供的

我正在构建一个狩猎应用程序,我需要从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;         }    }@OverrIDeprotected 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);}
总结

以上是内存溢出为你收集整理的将RSS feed解析为android应用全部内容,希望文章能够帮你解决将RSS feed解析为android应用所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/1083710.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-27
下一篇2022-05-27

发表评论

登录后才能评论

评论列表(0条)

    保存