Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-core/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何为Android构建RSS阅读器?_Android_Rss Reader - Fatal编程技术网

如何为Android构建RSS阅读器?

如何为Android构建RSS阅读器?,android,rss-reader,Android,Rss Reader,我是android新手,我正在尝试为android构建RSS阅读器。我已经构建了所有的类和XML文件,但是没有提供所需的输出。这只是在传达信息 没有可用的RSS源 请有人建议我该怎么做 下面是我从教程中获取并试图操作的代码- public final String RSSFEEDOFCHOICE = "http://blog.01synergy.com/feed/"; public final String tag = "RSSReader"; private RSSFeed feed = n

我是android新手,我正在尝试为android构建RSS阅读器。我已经构建了所有的类和XML文件,但是没有提供所需的输出。这只是在传达信息
没有可用的RSS源

请有人建议我该怎么做

下面是我从教程中获取并试图操作的代码-

public final String RSSFEEDOFCHOICE = "http://blog.01synergy.com/feed/";

public final String tag = "RSSReader";
private RSSFeed feed = null;

/** Called when the activity is first created. */

public void onCreate(Bundle icicle) {
    super.onCreate(icicle);
    setContentView(R.layout.main);

    // go get our feed!
    feed = getFeed(RSSFEEDOFCHOICE);

    // display UI
    UpdateDisplay();

}


private RSSFeed getFeed(String urlToRssFeed)
{
    try
    {
        // setup the url
       URL url = new URL(urlToRssFeed);

       // create the factory
       SAXParserFactory factory = SAXParserFactory.newInstance();
       // create a parser
       SAXParser parser = factory.newSAXParser();

       // create the reader (scanner)
       XMLReader xmlreader = parser.getXMLReader();
       // instantiate our handler
       RSSHandler theRssHandler = new RSSHandler();
       // assign our handler
       xmlreader.setContentHandler(theRssHandler);
       // get our data via the url class
       InputSource is = new InputSource(url.openStream());
       // perform the synchronous parse           
       xmlreader.parse(is);
       // get the results - should be a fully populated RSSFeed instance, or null on error
       return theRssHandler.getFeed();
    }
    catch (Exception ee)
    {
        // if we have a problem, simply return null
        return null;
    }
}
public boolean onCreateOptionsMenu(Menu menu) 
{
    super.onCreateOptionsMenu(menu);

    menu.add(0,0,0, "Choose RSS Feed");
    menu.add(0,1,0, "Refresh");
    Log.i(tag,"onCreateOptionsMenu");
    return true;
}

public boolean onOptionsItemSelected(Menu item){
    switch (((View) item).getId()) {
    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");
        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);

     startSubActivity(itemintent,0);
 }


private void startSubActivity(Intent itemintent, int i) {
    // TODO Auto-generated method stub

}

}
公共最终字符串RSSFEEDOFCHOICE=”http://blog.01synergy.com/feed/";
公共最终字符串tag=“RSSReader”;
专用RSSFeed feed=null;
/**在首次创建活动时调用*/
创建公共空间(捆绑冰柱){
超级冰柱;
setContentView(R.layout.main);
//去拿我们的饲料!
feed=getFeed(rssfeedof选择);
//显示用户界面
UpdateDisplay();
}
私有RSSFeed getFeed(字符串urlToRssFeed)
{
尝试
{
//设置url
URL=新URL(urlToRssFeed);
//创建工厂
SAXParserFactory=SAXParserFactory.newInstance();
//创建解析器
SAXParser parser=factory.newSAXParser();
//创建读卡器(扫描仪)
XMLReader=parser.getXMLReader();
//实例化我们的处理程序
RSSHandler-theRssHandler=新的RSSHandler();
//指派我们的处理人
setContentHandler(ThersHandler);
//通过url类获取我们的数据
InputSource is=新的InputSource(url.openStream());
//执行同步解析
parse(is);
//获取结果-应为完全填充的RSSFeed实例,或错误时为null
返回theRssHandler.getFeed();
}
捕获(异常ee)
{
//如果有问题,只需返回null
返回null;
}
}
公共布尔onCreateOptions菜单(菜单)
{
super.onCreateOptions菜单(菜单);
添加(0,0,0,“选择RSS提要”);
添加(0,1,0,“刷新”);
Log.i(标记“onCreateOptions菜单”);
返回true;
}
公共布尔值OnOptionItemSelected(菜单项){
开关(((视图)项).getId()){
案例0:
Log.i(标签,“设置RSS提要”);
返回true;
案例1:
Log.i(标签,“刷新RSS提要”);
返回true;
}
返回false;
}
私有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(“没有可用的RSS源”);
返回;
}
feedtitle.setText(feed.getTitle());
feedpubdate.setText(feed.getPubDate());
ArrayAdapter=newArrayAdapter(这是android.R.layout.simple_list_item_1,feed.getAllItems());
itemlist.setAdapter(适配器);
itemlist.setOnItemClickListener(此);
itemlist.setSelection(0);
}
public void onItemClick(AdapterView父视图、视图v、整型位置、长id)
{
Log.i(标记“单击的项”[“+feed.getItem(position.getTitle()+”]);
Intent itemintent=新的Intent(这个,showsdescription.class);
Bundle b=新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);
startSubActivity(itemintent,0);
}
私有无效开始活动(意向项目意向,int i){
//TODO自动生成的方法存根
}
}

检查以下链接,这是Android的开源RSS阅读器,您可以下载代码供参考

这里有一个关于如何构建Android RSS reder的教程(包括完整的源代码):

本教程使用SAX解析器,包括一个完整的Android项目,该项目访问RSS提要,然后在列表视图中显示提要


似乎仍然有很多人对此感兴趣——因此,如果你正在寻找一款Android 3.0+系统,它具有片段/异步任务等,以及完整的应用程序代码,我已经再次更新了帖子,这是我第一次使用RSS阅读器,它没有那么动态,有样板代码,但对我来说效果很好

用法:

RssParser parser = new RssParser(feedUrl);
Log.i("LOG", "Description: " + parser.getItem(3).description); //4th item's description
package com.uncocoder.course.app.feed_reader;

import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.xml.sax.Attributes;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;
import org.xml.sax.helpers.DefaultHandler;
import android.util.Log;


public class RssParser extends DefaultHandler {

    private StringBuilder   content;
    private boolean         inChannel;
    private boolean         inImage;
    private boolean         inItem;

    private ArrayList<Item> items   = new ArrayList<Item>();
    private Channel         channel = new Channel();

    private Item            lastItem;


    public RssParser(String url) {
        try {
            SAXParserFactory spf = SAXParserFactory.newInstance();
            SAXParser sp = spf.newSAXParser();
            XMLReader xr = sp.getXMLReader();
            URL sourceUrl = new URL(url);
            xr.setContentHandler(this);
            xr.parse(new InputSource(sourceUrl.openStream()));
        }
        catch (ParserConfigurationException e) {
            e.printStackTrace();
        }
        catch (SAXException e) {
            e.printStackTrace();
        }
        catch (IOException e) {
            e.printStackTrace();
        }
    }


    public class Item {

        public String title;
        public String description;
        public String link;
        public String category;
        public String pubDate;
        public String guid;
    }


    public class Channel {

        public String title;
        public String description;
        public String link;
        public String lastBuildDate;
        public String generator;
        public String imageUrl;
        public String imageTitle;
        public String imageLink;
        public String imageWidth;
        public String imageHeight;
        public String imageDescription;
        public String language;
        public String copyright;
        public String pubDate;
        public String category;
        public String ttl;
    }


    @Override
    public void startDocument() throws SAXException {
        Log.i("LOG", "StartDocument");
    }


    @Override
    public void endDocument() throws SAXException {
        Log.i("LOG", "EndDocument");
    }


    @Override
    public void startElement(String uri, String localName, String qName, Attributes atts) throws SAXException {
        if (localName.equalsIgnoreCase("image")) {
            inImage = true;
        }

        if (localName.equalsIgnoreCase("channel")) {
            inChannel = true;
        }

        if (localName.equalsIgnoreCase("item")) {
            lastItem = new Item();
            items.add(lastItem);
            inItem = true;
        }

        content = new StringBuilder();
    }


    @Override
    public void endElement(String uri, String localName, String qName) throws SAXException {
        if (localName.equalsIgnoreCase("image")) {
            inImage = false;
        }

        if (localName.equalsIgnoreCase("channel")) {
            inChannel = false;
        }

        if (localName.equalsIgnoreCase("item")) {
            inItem = false;
        }

        if (localName.equalsIgnoreCase("title")) {
            if (content == null) {
                return;
            }

            if (inItem) {
                lastItem.title = content.toString();
            } else if (inImage) {
                channel.imageTitle = content.toString();
            } else if (inChannel) {
                channel.title = content.toString();
            }

            content = null;
        }

        if (localName.equalsIgnoreCase("description")) {
            if (content == null) {
                return;
            }

            if (inItem) {
                lastItem.description = content.toString();
            } else if (inImage) {
                channel.imageDescription = content.toString();
            } else if (inChannel) {
                channel.description = content.toString();
            }

            content = null;
        }

        if (localName.equalsIgnoreCase("link")) {
            if (content == null) {
                return;
            }

            if (inItem) {
                lastItem.link = content.toString();
            } else if (inImage) {
                channel.imageLink = content.toString();
            } else if (inChannel) {
                channel.link = content.toString();
            }

            content = null;
        }

        if (localName.equalsIgnoreCase("category")) {
            if (content == null) {
                return;
            }

            if (inItem) {
                lastItem.category = content.toString();
            } else if (inChannel) {
                channel.category = content.toString();
            }

            content = null;
        }

        if (localName.equalsIgnoreCase("pubDate")) {
            if (content == null) {
                return;
            }

            if (inItem) {
                lastItem.pubDate = content.toString();
            } else if (inChannel) {
                channel.pubDate = content.toString();
            }

            content = null;
        }

        if (localName.equalsIgnoreCase("guid")) {
            if (content == null) {
                return;
            }

            lastItem.guid = content.toString();
            content = null;
        }

        if (localName.equalsIgnoreCase("url")) {
            if (content == null) {
                return;
            }

            channel.imageUrl = content.toString();
            content = null;
        }

        if (localName.equalsIgnoreCase("width")) {
            if (content == null) {
                return;
            }

            channel.imageWidth = content.toString();
            content = null;
        }

        if (localName.equalsIgnoreCase("height")) {
            if (content == null) {
                return;
            }

            channel.imageHeight = content.toString();
            content = null;
        }

        if (localName.equalsIgnoreCase("language")) {
            if (content == null) {
                return;
            }

            channel.language = content.toString();
            content = null;
        }

        if (localName.equalsIgnoreCase("copyright")) {
            if (content == null) {
                return;
            }

            channel.copyright = content.toString();
            content = null;
        }

        if (localName.equalsIgnoreCase("ttl")) {
            if (content == null) {
                return;
            }

            channel.ttl = content.toString();
            content = null;
        }
    }


    @Override
    public void characters(char[] ch, int start, int length) throws SAXException {
        if (content == null) {
            return;
        }

        content.append(ch, start, length);
    }


    public Item getItem(int index) {
        return items.get(index);
    }
}
类别:

RssParser parser = new RssParser(feedUrl);
Log.i("LOG", "Description: " + parser.getItem(3).description); //4th item's description
package com.uncocoder.course.app.feed_reader;

import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.xml.sax.Attributes;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;
import org.xml.sax.helpers.DefaultHandler;
import android.util.Log;


public class RssParser extends DefaultHandler {

    private StringBuilder   content;
    private boolean         inChannel;
    private boolean         inImage;
    private boolean         inItem;

    private ArrayList<Item> items   = new ArrayList<Item>();
    private Channel         channel = new Channel();

    private Item            lastItem;


    public RssParser(String url) {
        try {
            SAXParserFactory spf = SAXParserFactory.newInstance();
            SAXParser sp = spf.newSAXParser();
            XMLReader xr = sp.getXMLReader();
            URL sourceUrl = new URL(url);
            xr.setContentHandler(this);
            xr.parse(new InputSource(sourceUrl.openStream()));
        }
        catch (ParserConfigurationException e) {
            e.printStackTrace();
        }
        catch (SAXException e) {
            e.printStackTrace();
        }
        catch (IOException e) {
            e.printStackTrace();
        }
    }


    public class Item {

        public String title;
        public String description;
        public String link;
        public String category;
        public String pubDate;
        public String guid;
    }


    public class Channel {

        public String title;
        public String description;
        public String link;
        public String lastBuildDate;
        public String generator;
        public String imageUrl;
        public String imageTitle;
        public String imageLink;
        public String imageWidth;
        public String imageHeight;
        public String imageDescription;
        public String language;
        public String copyright;
        public String pubDate;
        public String category;
        public String ttl;
    }


    @Override
    public void startDocument() throws SAXException {
        Log.i("LOG", "StartDocument");
    }


    @Override
    public void endDocument() throws SAXException {
        Log.i("LOG", "EndDocument");
    }


    @Override
    public void startElement(String uri, String localName, String qName, Attributes atts) throws SAXException {
        if (localName.equalsIgnoreCase("image")) {
            inImage = true;
        }

        if (localName.equalsIgnoreCase("channel")) {
            inChannel = true;
        }

        if (localName.equalsIgnoreCase("item")) {
            lastItem = new Item();
            items.add(lastItem);
            inItem = true;
        }

        content = new StringBuilder();
    }


    @Override
    public void endElement(String uri, String localName, String qName) throws SAXException {
        if (localName.equalsIgnoreCase("image")) {
            inImage = false;
        }

        if (localName.equalsIgnoreCase("channel")) {
            inChannel = false;
        }

        if (localName.equalsIgnoreCase("item")) {
            inItem = false;
        }

        if (localName.equalsIgnoreCase("title")) {
            if (content == null) {
                return;
            }

            if (inItem) {
                lastItem.title = content.toString();
            } else if (inImage) {
                channel.imageTitle = content.toString();
            } else if (inChannel) {
                channel.title = content.toString();
            }

            content = null;
        }

        if (localName.equalsIgnoreCase("description")) {
            if (content == null) {
                return;
            }

            if (inItem) {
                lastItem.description = content.toString();
            } else if (inImage) {
                channel.imageDescription = content.toString();
            } else if (inChannel) {
                channel.description = content.toString();
            }

            content = null;
        }

        if (localName.equalsIgnoreCase("link")) {
            if (content == null) {
                return;
            }

            if (inItem) {
                lastItem.link = content.toString();
            } else if (inImage) {
                channel.imageLink = content.toString();
            } else if (inChannel) {
                channel.link = content.toString();
            }

            content = null;
        }

        if (localName.equalsIgnoreCase("category")) {
            if (content == null) {
                return;
            }

            if (inItem) {
                lastItem.category = content.toString();
            } else if (inChannel) {
                channel.category = content.toString();
            }

            content = null;
        }

        if (localName.equalsIgnoreCase("pubDate")) {
            if (content == null) {
                return;
            }

            if (inItem) {
                lastItem.pubDate = content.toString();
            } else if (inChannel) {
                channel.pubDate = content.toString();
            }

            content = null;
        }

        if (localName.equalsIgnoreCase("guid")) {
            if (content == null) {
                return;
            }

            lastItem.guid = content.toString();
            content = null;
        }

        if (localName.equalsIgnoreCase("url")) {
            if (content == null) {
                return;
            }

            channel.imageUrl = content.toString();
            content = null;
        }

        if (localName.equalsIgnoreCase("width")) {
            if (content == null) {
                return;
            }

            channel.imageWidth = content.toString();
            content = null;
        }

        if (localName.equalsIgnoreCase("height")) {
            if (content == null) {
                return;
            }

            channel.imageHeight = content.toString();
            content = null;
        }

        if (localName.equalsIgnoreCase("language")) {
            if (content == null) {
                return;
            }

            channel.language = content.toString();
            content = null;
        }

        if (localName.equalsIgnoreCase("copyright")) {
            if (content == null) {
                return;
            }

            channel.copyright = content.toString();
            content = null;
        }

        if (localName.equalsIgnoreCase("ttl")) {
            if (content == null) {
                return;
            }

            channel.ttl = content.toString();
            content = null;
        }
    }


    @Override
    public void characters(char[] ch, int start, int length) throws SAXException {
        if (content == null) {
            return;
        }

        content.append(ch, start, length);
    }


    public Item getItem(int index) {
        return items.get(index);
    }
}
package com.uncocoder.course.app.feed\u阅读器;
导入java.io.IOException;
导入java.net.URL;
导入java.util.ArrayList;
导入javax.xml.parsers.parserConfiguration异常;
导入javax.xml.parsers.SAXParser;
导入javax.xml.parsers.SAXParserFactory;
导入org.xml.sax.Attributes;
导入org.xml.sax.InputSource;
导入org.xml.sax.SAXException;
导入org.xml.sax.XMLReader;
导入org.xml.sax.helpers.DefaultHandler;
导入android.util.Log;
公共类RssParser扩展了DefaultHandler{
私有内容;
通道中的私有布尔;
私有布尔图像;
私有布尔inItem;
private ArrayList items=new ArrayList();
专用通道=新通道();
私人物品;
公共RssParser(字符串url){
试一试{
SAXParserFactory spf=SAXParserFactory.newInstance();
SAXParser sp=spf.newSAXParser();
XMLReader xr=sp.getXMLReader();
URL sourceUrl=新URL(URL);
xr.setContentHandler(此);
parse(新的InputSource(sourceUrl.openStream());
}
捕获(ParserConfiguration异常e){
e、 printStackTrace();
}
捕获(SAXE异常){
e、 printStackTrace();
}
捕获(IOE异常){
e、 printStackTrace();
}
}
公共类项目{
公共字符串标题;
公共字符串描述;
公共字符串链接;
公共字符串类别;
公共字符串pubDate;
公共字符串guid;