基本上我使用DOM Parser从列表视图中的RSS Feed中检索标题,描述,pubDate和缩略图。这是我的示例代码。
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;
}
}
}
前三个标签由我的解析器正确读取。但缩略图给我一个问题,并抛出异常......
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).
有任何问题吗? setImageURI(Uri.parse(currentItem.get( “IMAGEURL”)));
答案 0 :(得分:2)
错误是:
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).
在URL开始之前查看“/”。这导致了FileNotFoundException
。首先,请确保在回复中获得正确的网址。
要进行测试,请尝试使用硬编码的网址:
holder.articleImage.setImageURI(Uri.parse("http:/i.dailymail.co.uk/i/pix/2014/09/27/1411832985119_Puff_Image_galleryImage_SUNDERLAND_ENGLAND_SEPTEM.JPG"));
我想这应该可以正常工作。所以只需做出回应,你就可以了。
答案 1 :(得分:1)
是。你是对的!!!我不得不创建一个单独的AsyncTask来下载图像..
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);
}
}
}
还
new ImageDownloader(MyAdapter.this).execute(currentItem.get("imageURL"));
获取图像并将其显示在列表中。谢谢你的想法。他们给了我很多帮助!!