Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/233.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_Image_Rss - Fatal编程技术网

Android 从rss源获取图像

Android 从rss源获取图像,android,image,rss,Android,Image,Rss,我试图用Android Studio显示rss提要的图像,但我不知道怎么做。我可以得到所有其他项目,但我不知道为什么我不能得到的图像。你能帮我吗? 这是我的处理程序: 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; f

我试图用Android Studio显示rss提要的图像,但我不知道怎么做。我可以得到所有其他项目,但我不知道为什么我不能得到的图像。你能帮我吗? 这是我的
处理程序

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;
final int state_enclosure = 5;
final int state_url = 6;
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("url")){
        currentState = state_url;
    }
    //{
      //  if (!attributes.getValue("url").equalsIgnoreCase("null")) {
         //   feed.setImage(attributes.getValue("url"));
        //} else {
       //    feed.setImage("");
        //}
      //  currentState = state_image;
    //}
    else if (localName.equalsIgnoreCase("enclosure")){
        currentState = state_enclosure;
    }
    else if (localName.equalsIgnoreCase("title")){
        currentState = state_title;
    }
    else if (localName.equalsIgnoreCase("description")){
        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_url:
                item.setUrl(strCharacters);
                break;
            case state_enclosure:
                item.setEnclosure(strCharacters);
                break;
            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_url:
                feed.setUrl(strCharacters);
                break;
            case state_enclosure:
                feed.setEnclosure(strCharacters);
                break;
            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;
}
编辑 如何显示图像


您发布的代码与问题无关,您应该已经获取提要并对其进行解析以提取URL,然后您可以执行以下操作,但是我建议您使用来自Square的毕加索,它将完成所有繁重的工作,并添加缓存和自动中断请求等功能-


Android Studio只是您使用的IDE,与问题无关。只是为了更具体;)不管怎样,你知道答案吗?看看你的编辑为什么?这个解决方案是有效的,你认为你的新问题会改变这一点吗?至于编辑本身——非常糟糕——get视图可以被调用几十次,都在UI线程上——不使用该方法(或在UI线程上的任何时间)进行网络活动。一定要使用视图持有者模式来节省昂贵的查找视图调用和缓存!另外,您创建的“视图”参数和“视图行”是同一个参数-它是循环使用的检查视图是否为空,如果为空,则充气到该变量中,否则不要进行任何充气!我只是想告诉你我是如何在我的自定义列表视图中显示图像的(不要生气;))我是一个新手我一点也不生气,只是想强调我刚才所说的观点的重要性。如果你不注意,android会显示一个通知,说明应用程序没有响应,并询问用户是否关闭它如果你是新手,在你更好地熟悉操作系统之前,请留下像这样困难的事情,然后使用我上面链接的毕加索。我通常不喜欢只说使用第三方库的答案,因为它们只会鼓励辩论,但我在许多项目中使用毕加索——它很棒,重量轻,并且会解决我提到的一些问题
   try {
    URL url = new URL(web.get(position).getEnclosure()+web.get(position).getUrl());
      HttpGet httpRequest;

      httpRequest = new HttpGet(url.toURI());

      HttpClient httpclient = new DefaultHttpClient();
     HttpResponse response = httpclient
             .execute(httpRequest);

      HttpEntity entity = response.getEntity();
      BufferedHttpEntity b_entity = new BufferedHttpEntity(entity);
      InputStream input = b_entity.getContent();
      ImageView img = (ImageView) rowView.findViewById(R.id.enclosure);
      Bitmap bitmap = BitmapFactory.decodeStream(input);

        img.setImageBitmap(bitmap);

       } catch (Exception ex) {
         ex.printStackTrace();
       }
try {
    InputStream inStream = (InputStream) new URL(url).getContent();
    Drawable d = Drawable.createFromStream(inStream, "my source name");
    return d;
} catch (Exception e) {
    return null;
}