1

Basically I am using DOM Parser to retrieve the title,description,pubDate and thumbnail from an RSS Feed in a list view. Here is my sample code.

 public ArrayList<HashMap<String, String>> processXML(
                InputStream inputStream) throws Exception {
                DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory
                .newInstance();
                DocumentBuilder documentBuilder = documentBuilderFactory
                .newDocumentBuilder();
                Document xmlDocument = documentBuilder.parse(inputStream);
                Element rootElement = xmlDocument.getDocumentElement();
                L.m("" + rootElement.getTagName());
                NodeList itemsList = rootElement.getElementsByTagName("item");
                NodeList itemChildren = null;
                Node currentItem = null;
                Node currentChild = null;
                NamedNodeMap mediaThumbnailAttr = null;
                Node currentAttribute=null;
                int count = 0;
                ArrayList<HashMap<String, String>> results = new ArrayList<>();
                HashMap<String, String> currentMap = null;
                for (int i = 0; i < itemsList.getLength(); i++) {
                currentItem = itemsList.item(i);
                itemChildren = currentItem.getChildNodes();
                currentMap = new HashMap<>();
                for (int j = 0; j < itemChildren.getLength(); j++) {
                currentChild = itemChildren.item(j);
                if (currentChild.getNodeName().equalsIgnoreCase("title")) {
                // L.m(currentChild.getTextContent());
                currentMap.put("title", currentChild.getTextContent());
                }
                if (currentChild.getNodeName().equalsIgnoreCase("pubDate")) {
                // L.m(currentChild.getTextContent());
                currentMap
                .put("pubDate", currentChild.getTextContent());
                }
                if (currentChild.getNodeName().equalsIgnoreCase(
                "description")) {
                // L.m(currentChild.getTextContent());
                currentMap.put("description",
                currentChild.getTextContent());
                }

                if(currentChild.getNodeName().equalsIgnoreCase("media:thumbnail")){
                    //L.m(""+currentChild.getTextContent());
                    mediaThumbnailAttr = currentChild.getAttributes();
                    for(int k=0;k<mediaThumbnailAttr.getLength();
                            k++){
                        currentAttribute = mediaThumbnailAttr.item(k);
                        if(currentAttribute.getNodeName()
                                .equalsIgnoreCase("url")){
                            count++;

                            //L.m(currentChild.getAttributes().item(0).getTextContent());

                        }
                       //L.m();
                       currentMap.put("imageURL", currentChild
                .getAttributes().item(0).getTextContent());
                    }
                    count=0;
                }
                }
                if (currentMap != null && !currentMap.isEmpty()) {
                results.add(currentMap);
                }
                count = 0;
                }
                return results;
                }
                }
        }

The first three tags are read correctly by my parser..But the thumbnail is giving me a problem,and throws me an exception...

        Unable to decode stream: java.io.FileNotFoundException:   
           /http:/i.dailymail.co.uk/i/pix/2014/09/27/1411832985119_Puff_Image_galleryImage_SUNDERLAND_ENGLAND_SEPTEM.JPG: open failed: ENOENT (No such file or directory).

Is there any problem with setImageURI(Uri.parse(currentItem.get("imageURL")));

Theo
  • 3,099
  • 12
  • 53
  • 94
  • Just one thing which I can notice: extra "/" in beginning of Url. It should be "http://i.dailymail.co.uk/i/pix/2014/09/27/1411832985119_Puff_Image_galleryImage_SUNDERLAND_ENGLAND_SEPTEM.JPG". Not "/http://i.dailymail.co.uk/i/pix/2014/09/27/1411832985119_Puff_Image_galleryImage_SUNDERLAND_ENGLAND_SEPTEM.JPG" – MysticMagicϡ Sep 27 '14 at 17:38
  • I know. If I remove the holder.articleImage.setImageURI(Uri.parse(currentItem.get("imageURL"))); inside the getView method of my adapter I don't see any exceptions..But again the image does not appear on the list.. – Theo Sep 27 '14 at 17:42
  • Right.. But you are getting incorrect url. Please check that. For testing purpose, you can try hardcoded url like: `holder.articleImage.setImageURI(Uri.parse("http:/i.dailymail.co.uk/i/pix/2014/09/27/1411832985119_Puff_Image_galleryImage_SUNDERLAND_ENGLAND_SEPTEM.JPG"));` and check if image is displayed or not. @Theo – MysticMagicϡ Sep 27 '14 at 17:53
  • Nothing I am afraid. I did what you suggested and get this exception...09-27 13:56:40.349: E/AndroidRuntime(2282): FATAL EXCEPTION: main 09-27 13:56:40.349: E/AndroidRuntime(2282): java.lang.NullPointerException: uriString – Theo Sep 27 '14 at 17:59

2 Answers2

2

The error is:

Unable to decode stream: java.io.FileNotFoundException:   
       /http:/i.dailymail.co.uk/i/pix/2014/09/27/1411832985119_Puff_Image_galleryImage_SUNDERLAND_ENGLAND_SEPTEM.JPG: open failed: ENOENT (No such file or directory).

See the "/" before the URL starts. That is causing the FileNotFoundException. So firstly, make sure that you get correct URL in your response.

And for testing, try with a hardcoded url:

holder.articleImage.setImageURI(Uri.parse("http:/i.dailymail.co.uk/i/pix/2014/0‌​9/27/1411832985119_Puff_Image_galleryImage_SUNDERLAND_ENGLAND_SEPTEM.JPG"));

I guess that should work fine. So just make correction in response, and you are good to go.

MysticMagicϡ
  • 28,593
  • 16
  • 73
  • 124
  • If I comment out the L.m(currentChild.getAttributes().item(0).getTextContent()); which is inside the if(currentChild.getNodeName().equalsIgnoreCase("media:thumbnail")){},I read the images in the log file..That part is ok. I put the link of the image image inside the Uri.parse() as you showed me but no luck yet. – Theo Sep 27 '14 at 18:08
  • image not shown..Also an exception .....resolveUri failed on bad bitmap uri: http:/i.dailymail.co.uk/i/pix/2014/0??9/27/1411832985119_Puff_Image_galleryImage_SUNDERLAND_ENGLAND_SEPTEM.JPG – Theo Sep 27 '14 at 18:13
  • I get the same exception as before....Unable to decode stream: java.io.FileNotFoundException: /http:/i.dailymail.co.uk/i/pix/2014/09/27/1411832985119_Puff_Image_galleryImage_SUNDERLAND_ENGLAND_SEPTEM.JPG: open failed: ENOENT (No such file or directory)..So there is something wrong with the setImageURI method..hmm... – Theo Sep 27 '14 at 18:19
  • May be @Theo .. see if [this](http://stackoverflow.com/a/14054545/1777090) helps. – MysticMagicϡ Sep 27 '14 at 18:21
  • Is there any tutorial on how you can load url images on a listview? I didn't find something good..Only about lazy lists and so on... – Theo Sep 27 '14 at 18:25
  • See [this](http://www.learn2crack.com/2014/06/android-load-image-from-internet.html). Firstly, you would need to get bitmap and then use that to show image. @Theo – MysticMagicϡ Sep 27 '14 at 18:32
  • That would change the whole program as that sample is about only a single image. I want to download and update the images is each row along with titles and descriptions...from here...http://www.dailymail.co.uk/sport/teampages/sunderland.rss – Theo Sep 27 '14 at 18:41
1

Yes. You were right!!! I had to create a separate AsyncTask for downloading the images..

 public class ImageDownloader extends AsyncTask<String, String, Bitmap> {

   private MyAdapter parentActivity;


   public ImageDownloader(MyAdapter parentActivity) {
    super();

    this.parentActivity = parentActivity;
 }

  protected Bitmap doInBackground(String... args) {
    try {
        Bitmap bitmap = BitmapFactory.decodeStream((InputStream)new URL(args[0]).getContent());
        return bitmap;
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
 }

   protected void onPostExecute(Bitmap image) {
    if(image != null){
        parentActivity.updateBitmap(image);

    }
  }
}

And also

   new ImageDownloader(MyAdapter.this).execute(currentItem.get("imageURL"));

to get the images and show them to the list. Thanks for your ideas. They helped me a lot!!

Theo
  • 3,099
  • 12
  • 53
  • 94