Android:只从rss网站上下载最新文章的图片

时间:2011-07-15 02:09:52

标签: android image rss

我想要一个定期检查网站rss的应用。并且只将最新文章中的图像下载到SD卡。我怎么能这样做你能给我一些代码或地方来学习这个。

1 个答案:

答案 0 :(得分:1)

独自一人是危险的 - 接受这个。这是一个简单的xml解析器,它将已解析的项添加到ArrayList。例如,您可以稍后在某个适配器中使用这些项目。

只需修改此代码即可适合您的Feed。用法很简单 - readRss(1);只读取Feed中的第一项。关于如何将图像下载到SD卡 - 周围有很多例子。

private XmlPullParserFactory factory = null;
private XmlPullParser xpp = null;
private ArrayList<String> newsId;
private ArrayList<String> newsTitle;
private ArrayList<String> newsDescription;
private ArrayList<String> newsImage;


 private boolean connect() {
        try {
            if (xpp == null) {
                URL url = new URL(feedString);
                URLConnection connection = url.openConnection();

                HttpURLConnection httpConnection = (HttpURLConnection)connection; 
                int responseCode = httpConnection.getResponseCode(); 

                if (responseCode == HttpURLConnection.HTTP_OK) {
                    InputStream in = httpConnection.getInputStream();

                    factory = XmlPullParserFactory.newInstance();
                    factory.setNamespaceAware(true);
                    xpp = factory.newPullParser();

                    xpp.setInput(in, "UTF-8");
                }
            }

            return true;

        } catch (XmlPullParserException xe) {
            Log.e("RssReader", "Connection", xe);
            return false;

        } catch (IOException eio) {
            Log.e("RssReader", "Connection", eio);
            return false;

        }
    }


    private void readRss(int itemsToRead) {
        boolean insideItem = false;
        boolean insideTitle = false;
        boolean insideImage = false;
        boolean insideDescription = false;
        boolean insideId = false;

        try {
            if (connect()) {
                int eventType = xpp.getEventType();

                while (eventType != XmlPullParser.END_DOCUMENT
                        && itemsToRead >= 0) {
                    if (eventType == XmlPullParser.START_TAG) {
                        if (xpp.getName().equalsIgnoreCase("item")) {               //news tag
                            insideItem = true;
                        }  else if (insideItem
                                && xpp.getName().equalsIgnoreCase("original")) {
                            insideImage = true;
                        } else if (insideItem
                                && xpp.getName()
                                        .equalsIgnoreCase("description")) {
                            insideDescription = true;
                        } else if (insideItem
                                && xpp.getName().equalsIgnoreCase("id")) {
                            insideId = true;
                        }

                    } else if (eventType == XmlPullParser.END_TAG) {
                        if (xpp.getName().equalsIgnoreCase("item")) {
                            insideItem = false;
                            --itemsToRead;
                        }

                    } else if (eventType == XmlPullParser.TEXT && insideItem) {
                        if (xpp.getText() != null) {
                            if (insideTitle) {
                                newsTitle.add(xpp.getText());
                                insideTitle = false;
                            } else if (insideImage) {
                                newsImage.add(xpp.getText());
                                insideImage = false;
                            } else if (insideDescription) {
                                newsDescription.add(xpp.getText());
                                insideDescription = false;
                            } else if (insideId) {
                                newsId.add(xpp.getText());
                                insideId = false;
                            }
                        }

                    }

                    eventType = xpp.next();
                }
            }

        } catch (XmlPullParserException e) {
            Log.e("RssReader", "Connection", e);
        } catch (IOException e) {
            Log.e("RssReader", "Connection", e);
        }
    }