Reputation: 3149
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")));
Upvotes: 1
Views: 7299
Reputation: 3149
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!!
Upvotes: 1
Reputation: 28823
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/09/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.
Upvotes: 2