如何从FeedBurner URL获取图像表格RSS feed?

时间:2012-11-02 18:18:47

标签: android rss

我正在尝试从FeedBurner网址获取图片,但我无法完成此操作。

编辑:

这是URL

我注意到图像在<内容:编码>标签

我也无法从此URL检索图片。

我无法从中获取信息。

这是我的代码:

class RSSHandler extends DefaultHandler {

private Post currentPost = new Post();

StringBuffer chars = new StringBuffer();

@Override
public void startElement(String uri, String localName, String qName,
        Attributes atts) {

    chars = new StringBuffer();


    if (localName.equalsIgnoreCase("thumbnail")) {          
        String thumbnail = atts.getValue("url");    
        currentPost.setThumbnail(thumbnail);
    }
}

@Override
public void endElement(String uri, String localName, String qName)
        throws SAXException {

    if (localName.equalsIgnoreCase("title")
            && currentPost.getTitle() == null) {
        currentPost.setTitle(chars.toString());

    }
    String pubd;
    if (localName.equalsIgnoreCase("pubDate")
            && currentPost.getPubDate() == null) {

        currentPost.setPubDate(chars.toString());
        Home.DATE_1.add(chars.toString()); 
    }

    if (localName.equalsIgnoreCase("link") && currentPost.getUrl() == null) {

        currentPost.setUrl(chars.toString());
    }

    if (localName.equalsIgnoreCase("item")) {
        FragmentView.PostList.add(currentPost);

        currentPost = new Post();
        }

}

@Override
public void characters(char ch[], int start, int length) {
    chars.append(new String(ch, start, length));
}

}

我正在使用此处理程序从RSS源获取信息。一切都适用于其他Feed,但当它是FeedBurner网址时,我似乎无法获取图片。

2 个答案:

答案 0 :(得分:0)

首先,您需要获取图片的网址

<media:content url="http://thenextweb.com/wp-content/blogs.dir/1/files/2012/11/SC20121102-150432.png" medium="image">
<media:title type="html">SC20121102-150432</media:title>
<media:thumbnail url="http://thenextweb.com/wp-content/blogs.dir/1/files/2012/11/SC20121102-150432-150x150.png" />

将图像转换为位图后,示例代码:

public Bitmap getBitmapFromURL(String src) {
    try 
    {
        URL url = new URL(src);
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        connection.setDoInput(true);
        connection.connect();
        InputStream input = connection.getInputStream();
        return BitmapFactory.decodeStream(input);
    } 
    catch (IOException e) 
    {
        //TODO on error
        return null;
    }
}

编辑:

首先阅读您的HTML文件,然后使用正则表达式获取带有IMG标签的src网址

正则表达式:

String imgRegex = "<img[^>]+src\\s*=\\s*['\"]([^'\"]+)['\"][^>]*>";

使用imgregex值获取位图图像。

答案 1 :(得分:0)

您可以通过正则表达式从内容中提取图像,但如果您使用的是XmlPullParser,则首选。

这个库有一个很好的例子: https://github.com/Pkmmte/PkRSS

此代码段位于该库的Rss2Parser.java文件中,它可以完全满足您的需求。只需将内容作为参数传递即可。

/ **

 * Pulls an image URL from an encoded String.

 *

 * @param encoded The String which to extract an image URL from.

 * @return The first image URL found on the encoded String. May return an

 * empty String if none were found.

 */

private String pullImageLink(String encoded) {

    try {

        XmlPullParserFactory factory = XmlPullParserFactory.newInstance();

        XmlPullParser xpp = factory.newPullParser();


        xpp.setInput(new StringReader(encoded));

        int eventType = xpp.getEventType();

        while (eventType != XmlPullParser.END_DOCUMENT) {

            if (eventType == XmlPullParser.START_TAG && "img".equals(xpp.getName())) {

                int count = xpp.getAttributeCount();

                for (int x = 0; x < count; x++) {

                    if (xpp.getAttributeName(x).equalsIgnoreCase("src"))

                        return xpp.getAttributeValue(x).replaceAll("-110x52", "");

                }

            }

            eventType = xpp.next();

        }

    }

    catch (Exception e) {

        log(TAG, "Error pulling image link from description!\n" + e.getMessage(), Log.WARN);

    }


    return "";

}