1

I'm trying to find yout what's the way to show a set of images from this link www.repubblica.it/rss/tecnologia/rss2.0.xml. I have to show them in my RSS app but I'm stuck on this argument. The images are in this tag <enclosure url="http://www.repstatic.it/content/nazionale/img/2014/02/06/201914230-3e1f0f4a-c5e4-413a-acd0-f15b781438eb.jpg" length="24317" type="image/jpeg"/> (for example). Can you help me? Any help is appreciated. This is my 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;
 }
 }
Pier
  • 794
  • 1
  • 12
  • 27

1 Answers1

2

try this code :

in this method

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

add this code will return the url from the element :

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

feed me back

mohammed momn
  • 3,230
  • 1
  • 20
  • 16
  • And about how to show them? Do I have to add _final int state_enclosure_ and in _public vois chatacter()_ method add this: _case state_enclosure: item.setEnclosure(strCharacters); break;_ ; case state_title: feed.setTitle(strCharacters); break; – Pier Feb 07 '14 at 22:01
  • if you need only the url you don't need implement chatacter() , you will get the value from url in startElement method you will need to save it in array and after parsing download them and you can display them on your views , what do you mean with state_enclosure ?? – mohammed momn Feb 07 '14 at 22:06
  • Nothing, I was confused. So then to show them in my ArrayAdapter it's good this code? – Pier Feb 07 '14 at 22:26
  • _try { URL url = new URL("what's here?"); HttpGet httpRequest = null; httpRequest = new HttpGet(url.toURI()); HttpClient httpclient = new DefaultHttpClient(); HttpResponse response = (HttpResponse) httpclient .execute(httpRequest); HttpEntity entity = response.getEntity(); BufferedHttpEntity b_entity = new BufferedHttpEntity(entity); InputStream input = b_entity.getContent(); Bitmap bitmap = BitmapFactory.decodeStream(input); img.setImageBitmap(bitmap); } catch (Exception ex) { }_ – Pier Feb 07 '14 at 22:27
  • above code will get you the url it self and then you need to download them try this example to download image from url http://androidexample.com/Download_Images_From_Web_And_Lazy_Load_In_ListView_-_Android_Example/index.php?view=article_discription&aid=112&aaid=134 – mohammed momn Feb 07 '14 at 22:29
  • Is there a shorter way to show them? Because I really have many classes and in this example there are other 5 classes – Pier Feb 07 '14 at 22:38
  • you can make AsnycTask class in your Activity that contain the ListView and download them in this class – mohammed momn Feb 07 '14 at 22:58
  • ok, I'll do it, you've been very helpful thank you. The last thing I want to ask you if is this what I need by AsyncTask: http://stackoverflow.com/questions/8224993/loading-image-using-asynctask – Pier Feb 07 '14 at 23:07
  • yea but your AsyncTask will take Array of String that you get from your parser as parameters to download them , could you mark the answer a accepted :) ? – mohammed momn Feb 07 '14 at 23:10
  • Yes, I really thank you for you help :)) (if I have another little question can I comment it here?) – Pier Feb 07 '14 at 23:28
  • sure :) , and you can post new question and feel free to mention me – mohammed momn Feb 07 '14 at 23:30