2

Hello Everybody I am relatively new to Android Development and I am creating an RSS Reader for a news site. The problem I have is that site with the rss feed of which I want to get images is of a type "< img src="http......"" What I have done for the code on android is ...2 classes ...RssItem and RssItem Displayer

public class RssItem {

private String title;
private String description;
private Date pubDate;
private String link;
private static ImageView image;

public RssItem(String title, String description,ImageView image, Date pubDate, String link) {
    this.title = title;
    this.description = description;
    RssItem.image = image;
    this.pubDate = pubDate;
    this.link = link;
}

public String getTitle() {
    return title;
}

public void setTitle(String title) {
    this.title = title;
}

public String getDescription() {
    return description;
}

public void setDescription(String description) {
    this.description = description;
}
public ImageView getImage(ImageView image) {
      return this.image = image;
}     
public void setImage(ImageView image) {
          this.image = image;     
}
public Date getPubDate() {
    return pubDate;
}

public void setPubDate(Date pubDate) {
    this.pubDate = pubDate;
}

public String getLink() {
    return link;
}

public void setLink(String link) {
    this.link = link;
}

@Override
public String toString() {

    SimpleDateFormat sdf = new SimpleDateFormat("MM/dd - hh:mm:ss");

    String result = getTitle() + "   ( " + sdf.format(this.getPubDate()) + " )";
    return result;
}

public static ArrayList<RssItem> getRssItems(String feedUrl) {

    ArrayList<RssItem> rssItems = new ArrayList<RssItem>();

    try {
        //open an URL connection make GET to the server and 
        //take xml RSS data
        URL url = new URL(feedUrl);
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();

        if (conn.getResponseCode() == HttpURLConnection.HTTP_OK) {
            InputStream is = conn.getInputStream();

            //DocumentBuilderFactory, DocumentBuilder are used for 
            //xml parsing
            DocumentBuilderFactory dbf = DocumentBuilderFactory
                    .newInstance();
            DocumentBuilder db = dbf.newDocumentBuilder();

            //using db (Document Builder) parse xml data and assign
            //it to Element
            Document document = db.parse(is);
            Element element = document.getDocumentElement();

            //take rss nodes to NodeList
            NodeList nodeList = element.getElementsByTagName("item");

            if (nodeList.getLength() > 0) {
                for (int i = 0; i < nodeList.getLength(); i++) {

                    //take each entry (corresponds to <item></item> tags in 
                    //xml data

                    Element entry = (Element) nodeList.item(i);

                    Element _titleE = (Element) entry.getElementsByTagName(
                            "title").item(0);
                    Element _descriptionE = (Element) entry
                            .getElementsByTagName("description").item(0);
                    Element _imageE = (Element) entry
                            .getElementsByTagName("image").item(0);
                    Element _pubDateE = (Element) entry
                            .getElementsByTagName("pubDate").item(0);
                    Element _linkE = (Element) entry.getElementsByTagName(
                            "link").item(0);

                    String _title = _titleE.getFirstChild().getNodeValue();
                    String _description = _descriptionE.getFirstChild().getNodeValue();

                     // ImageView image = (ImageView)findViewbyId(R.id.MyImage);
                     // Bitmap bitmap = BitmapFactory.decodeStream((InputStream)new URL(feedUrl).getContent());
                     // image.setImageBitmap(bitmap); 
                    //} catch (MalformedURLException e) {

                    /*try {
                          //where imageUrl is what you pulled out from the rss feed
                          Bitmap bitmap = BitmapFactory.decodeStream((InputStream)new URL(feedUrl).getContent());
                          image.setImageBitmap(bitmap); 
                        } catch (MalformedURLException e) {
                         //log exception here
                        } catch (IOException e) {
                          //log exception here
                        }

                    */

                    Date _pubDate = new Date(_pubDateE.getFirstChild().getNodeValue());
                    String _link = _linkE.getFirstChild().getNodeValue();

                    //create RssItemObject and add it to the ArrayList
                    RssItem rssItem = new RssItem(_title, _description, image,
                            _pubDate, _link);

                    rssItems.add(rssItem);
                }
            }

        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return rssItems;
}      

but I do not know how to put the src image element

I also create an imageview that will show it on the layout xml

In the other class that shows RSS item displayer I have

public class RssItemDisplayer extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.rss_item_displayer);

    RssItem selectedRssItem = com.AndroidRSSReader.AndroidRSSReader.selectedRssItem;
    //Bundle extras = getIntent().getExtras();
    TextView titleTv = (TextView)findViewById(R.id.titleTextView);  
    TextView contentTv = (TextView)findViewById(R.id.contentTextView);  
    ImageView image=(ImageView)findViewById(R.id.MyImage);  

    String title = "";
    SimpleDateFormat sdf = new SimpleDateFormat("MM/dd - hh:mm:ss");
    title = "\n" + selectedRssItem.getTitle() + "   ( "
     + sdf.format(selectedRssItem.getPubDate()) + " )\n\n";

    String content = "";
    content += selectedRssItem.getDescription() + "\n"
            + selectedRssItem.getLink();

    titleTv.setText(title);
    contentTv.setText(content);

    image=selectedRssItem.getImage(image);
    try {
          String feedUrl = null;
        //where imageUrl is what you pulled out from the rss feed
          Bitmap bitmap = BitmapFactory.decodeStream((InputStream)new URL(feedUrl).getContent());
          image.setImageBitmap(bitmap); 
        } catch (MalformedURLException e) {
         //log exception here
        } catch (IOException e) {
          //log exception here
        }

the logcat gave me warnins to the line of rss item that bitmap is get in the code

11-29 00:13:44.593: WARN/System.err(2997): java.net.MalformedURLException: Protocol not found: 
11-29 00:13:44.593: WARN/System.err(2997):     at java.net.URL.<init>(URL.java:275)
11-29 00:13:44.593: WARN/System.err(2997):     at java.net.URL.<init>(URL.java:159)
11-29 00:13:44.593: WARN/System.err(2997):     at com.AndroidRSSReader.RssItem.getRssItems(RssItem.java:92)
11-29 00:13:44.593: WARN/System.err(2997):     at com.AndroidRSSReader.AndroidRSSReader.refressRssList(AndroidRSSReader.java:301)
11-29 00:13:44.593: WARN/System.err(2997):     at com.AndroidRSSReader.AndroidRSSReader.onCreate(AndroidRSSReader.java:229)
11-29 00:13:44.593: WARN/System.err(2997):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
11-29 00:13:44.593: WARN/System.err(2997):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
11-29 00:13:44.593: WARN/System.err(2997):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
11-29 00:13:44.593: WARN/System.err(2997):     at android.app.ActivityThread.access$2300(ActivityThread.java:125)
11-29 00:13:44.603: WARN/System.err(2997):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
11-29 00:13:44.603: WARN/System.err(2997):     at android.os.Handler.dispatchMessage(Handler.java:99)
11-29 00:13:44.613: WARN/System.err(2997):     at android.os.Looper.loop(Looper.java:123)
11-29 00:13:44.613: WARN/System.err(2997):     at android.app.ActivityThread.main(ActivityThread.java:4627)
11-29 00:13:44.613: WARN/System.err(2997):     at java.lang.reflect.Method.invokeNative(Native Method)
11-29 00:13:44.613: WARN/System.err(2997):     at java.lang.reflect.Method.invoke(Method.java:521)
11-29 00:13:44.613: WARN/System.err(2997):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:876)
11-29 00:13:44.613: WARN/System.err(2997):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:634)
11-29 00:13:44.613: WARN/System.err(2997):     at dalvik.system.NativeStart.main(Native Method)

What should I put in those two things to grub the image and especially to rss item class Anyway If someone can help i will be grateful

Kevin Coppock
  • 133,643
  • 45
  • 263
  • 274
DroidAjax
  • 48
  • 1
  • 6
  • It's not clear what you mean by "but I do not know how to put the src image element". Can you please describe this better? Also, some of your question did not get formatted as code when it should have, which makes it difficult to read. – Eric Levine Nov 28 '10 at 16:42
  • the rss feed has the images in this format < img src="http......"" – DroidAjax Nov 28 '10 at 16:59
  • I understand that. What is the problem? My guess is you want to take that image URL and display it in an android.widget.ImageView. Is that correct? – Eric Levine Nov 28 '10 at 17:01
  • Yes exactly that ....What should I put to the Element and in the part that creates the RSS Item Object – DroidAjax Nov 28 '10 at 17:10
  • It thinks that the image is String and gives me errors ...Any help please – DroidAjax Nov 28 '10 at 17:37
  • elevine is right about displaying the image. Could you post an example of what the RSS feed you are parsing looks like? I've used Jsoup successfully for parsing this kind of data out of HTML. I'm guessing it would work for RSS as well: http://jsoup.org/ – Computerish Nov 28 '10 at 17:45
  • 1
    The XML is only text, the src attribute simply contains a URL to the image. You'll need to just set the image field to be a string, and handle downloading the image as a bitmap elsewhere (preferably in an AsyncTask to prevent UI blocking). As far as getting the src attribute, you're going to want to get the `img` element, and call `.getAttribute("src")` on it. – Kevin Coppock Nov 28 '10 at 17:45
  • This line in your code should probably be removed: image=selectedRssItem.getImage(image); – Eric Levine Nov 28 '10 at 21:12
  • Can you paste the logcat warnings into your question? – Eric Levine Nov 28 '10 at 21:13
  • In your code above, feedUrl is always null. You should set it to the URL of the image you want to retrieve. Try setting some break points and using debug mode to see the values of your variables. – Eric Levine Nov 28 '10 at 22:40
  • The point is that I want the code to grab the url of the image.So I can not set a url by myself.As I stated before in the rss xml feeds the image is in this style "< img src="http......"" – DroidAjax Nov 28 '10 at 22:58
  • See the edits to my answer below. – Eric Levine Nov 29 '10 at 01:51
  • Ok I solved with the help of eveline and the idea of kcoppock ....I get the image url with java regular expression and the I used this... try { //where imageUrl is what you pulled out from the rss feed Bitmap bitmap = BitmapFactory.decodeStream((InputStream)new URL(imageUrl).getContent()); image.setImageBitmap(bitmap); } catch (MalformedURLException e) { //log exception here } catch (IOException e) { //log exception here } – DroidAjax Nov 29 '10 at 17:48

1 Answers1

3

Your RssItem class should not have the following:

private static ImageView image;

Replace that with:

private String imageUrl;

In your getRssItems method, use _imageE to get the value for imageUrl. Then use imageUrl as described below.

An ImageView will only display images stored locally on your device, so setting it to remote URL will not work. One solution can be found here: Android, Make an image at a URL equal to ImageView's image So, you will want to change from:

titleTv.setText(title);
image.setImageURI(uri);
contentTv.setText(content);

to:

titleTv.setText(title);
contentTv.setText(content);
try {
  //where imageUrl is what you pulled out from the rss feed
  Bitmap bitmap = BitmapFactory.decodeStream((InputStream)new URL(imageUrl).getContent());
  image.setImageBitmap(bitmap); 
} catch (MalformedURLException e) {
 //log exception here
} catch (IOException e) {
  //log exception here
}
Community
  • 1
  • 1
Eric Levine
  • 13,536
  • 5
  • 49
  • 49
  • 1
    I tried to put your answer to my code but it change it to bitmap...and I am not sure how to implement it.I suppose in the part that says new URL(imageUrl) i put feedUrl .... – DroidAjax Nov 28 '10 at 18:17
  • yeah but this is for the RSS Diplayer class which I don't think it is right In the other class though RSS item is with no errors although in the logcat I have a warning...In the Rss Displayer class where the image will actually show up in the layout (Imageview ) the Url must set again .... – DroidAjax Nov 28 '10 at 19:22
  • Anybody Please help guys is not so difficult I suppose – DroidAjax Nov 28 '10 at 20:53