java - 从 URL 获取并显示一系列图像

标签 java android image url rss

我正试图从这个链接中找到显示一组图像的方法 www.repubblica.it/rss/tecnologia/rss2.0.xml .我必须在我的 RSS 应用程序中显示它们,但我坚持这个论点。 图片在此标签中 <enclosure url="http://www.repstatic.it/content/nazionale/img/2014/02/06/201914230-3e1f0f4a-c5e4-413a-acd0-f15b781438eb.jpg" length="24317" type="image/jpeg"/> (例如)。 你能帮助我吗?任何帮助表示赞赏。 这是我的Handler :

public class RSSHandler extends DefaultHandler {

 final int state_unknown = 0;
 final int state_title = 1;
 final int state_description = 2;
 final int state_link = 3;
 final int state_pubdate = 4;
 int currentState = state_unknown;

 RSSFeed feed;
 RSSItem item;

 boolean itemFound = false;

 RSSHandler(){
 }

 RSSFeed getFeed(){
 return feed;
 }

 @Override
 public void startDocument() throws SAXException {
 // TODO Auto-generated method stub
 feed = new RSSFeed();
 item = new RSSItem();

 }

 @Override
 public void endDocument() throws SAXException {
 // TODO Auto-generated method stub
 }

 @Override
 public void startElement(String uri, String localName, String qName,
 Attributes attributes) throws SAXException {
 // TODO Auto-generated method stub

 if (localName.equalsIgnoreCase("item")){
 itemFound = true;
 item = new RSSItem();
 currentState = state_unknown;
 }
 else if (localName.equalsIgnoreCase("title")){
 currentState = state_title;
 }
 else if (localName.equalsIgnoreCase("psi")){
 currentState = state_description;
 }
 else if (localName.equalsIgnoreCase("link")){
 currentState = state_link;
 }
 else if (localName.equalsIgnoreCase("pubdate")){
 currentState = state_pubdate;
 }
 else{
 currentState = state_unknown;
 }

 }

 @Override
 public void endElement(String uri, String localName, String qName)
 throws SAXException {
 // TODO Auto-generated method stub
 if (localName.equalsIgnoreCase("item")){
 feed.addItem(item);
 }
 }

  @Override
  public void characters(char[] ch, int start, int length)
  throws SAXException {
  // TODO Auto-generated method stub

  String strCharacters = new String(ch,start,length);

  if (itemFound==true){
  // "item" tag found, it's item's parameter
  switch(currentState){
  case state_title:
  item.setTitle(strCharacters);
  break;
 case state_description:
 item.setDescription(strCharacters);
 break;
 case state_link:
 item.setLink(strCharacters);
 break;
 case state_pubdate:
 item.setPubdate(strCharacters);
 break;
 default:
 break;
 }
 }
 else{
 // not "item" tag found, it's feed's parameter
 switch(currentState){
 case state_title:
 feed.setTitle(strCharacters);
 break;
 case state_description:
 feed.setDescription(strCharacters);
 break;
 case state_link:
 feed.setLink(strCharacters);
 break;
 case state_pubdate:
 feed.setPubdate(strCharacters);
 break;
 default:
  break;
 }
 }

  currentState = state_unknown;
 }
 }

最佳答案

试试这段代码:

在这个方法中

public void startElement(String uri, String localName, String qName,Attributes attributes)

添加此代码将从元素返回 url :

if ("enclosure".equals(qName)) {
        for (int i = 0; i < attributes.getLength(); i++)
            if (attributes.getQName(i).equals("url"))
                String url = attributes.getValue(i);

给我反馈

关于java - 从 URL 获取并显示一系列图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21638199/

相关文章:

java - java中的mongodb嵌套搜索查询

java - Toast 和 ProgressDialog 均不出现

html - iframe 中的样式图像

java - 使用 Maven 在 Nexus 上部署外部库

java - Spring mvc : how to pass parameters in mvc without method arguments at the controller

java - 套接字和 setSoTimeout()

android - 在显示布局之前检测 TextVIew 是否被椭圆化

android - Ionic 2 侧边菜单在按下数次后不起作用

java - 以最佳性能将图像写入 servlet 响应

html - 网站图片不更新