Skip to content

NasaDailyImage RSS feed  #2

@GoogleCodeExporter

Description

@GoogleCodeExporter
What steps will reproduce the problem?

i am providing sourcecodes in NasaDailyImage.java according to the book(head 
first android)
there were no errors in code but i am not getting the output on the emulator. 
it is showing blank.
 source code which i am providing according to the book is

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;

import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.xml.sax.Attributes;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;
import org.xml.sax.helpers.DefaultHandler;

import android.os.Bundle;
import android.widget.ImageView;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.view.Menu;
import android.widget.TextView;
import com.example.nasadailyimage.R;

public class NasaDailyImage extends Activity {

     @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_nasa_daily_image);
            IotdHandler handler = new IotdHandler ();
            handler.processFeed();
            resetDisplay (handler.getTitle(), handler.getDate(), handler.getImage(), handler.getDescription());
        }

        public class IotdHandler extends DefaultHandler {
            private String url = "http://www.nasa.gov/rss/image_of_the_day.rss";
            private boolean inUrl = false;
            private boolean inTitle = false;
            private boolean inDescription = false;
            private boolean inItem = false;
            private boolean inDate = false;
            private Bitmap image = null;
            private String title = null;
            private StringBuffer description = new StringBuffer();
            private String date = null;


            public void processFeed() {
                try {
                SAXParserFactory factory =
                SAXParserFactory.newInstance();
                SAXParser parser = factory.newSAXParser();
                XMLReader reader = parser.getXMLReader();
                reader.setContentHandler(this);
                InputStream inputStream = new URL(url).openStream();
                reader.parse(new InputSource(inputStream));
                } catch (Exception e) {  }
            }

                private Bitmap getBitmap(String url) {
                    try {
                    HttpURLConnection connection = (HttpURLConnection)new URL(url).openConnection();
                    connection.setDoInput(true);
                    connection.connect();
                    InputStream input = connection.getInputStream();
                    Bitmap bilde = BitmapFactory.decodeStream(input);
                    input.close();
                    return bilde;
                    } catch (IOException ioe) { return null; }
                    }

                public void startElement(String url, String localName, String qName, Attributes attributes) throws SAXException {
                        if (localName.endsWith(".jpg")) { inUrl = true; }
                        else { inUrl = false; }

                        if (localName.startsWith("item")) { inItem = true; }
                        else if (inItem) {

                            if (localName.equals("title")) { inTitle = true; }
                            else { inTitle = false; }

                            if (localName.equals("description")) { inDescription = true; }
                            else { inDescription = false; }

                            if (localName.equals("pubDate")) { inDate = true; }
                            else { inDate = false; }
                            }
                        }


                public void characters(char ch[], int start, int length) { String chars = new String(ch).substring(start, start + length);
                    if (inUrl && url == null) { image = getBitmap(chars); }
                    if (inTitle && title == null) { title = chars; }
                    if (inDescription) { description.append(chars); }
                    if (inDate && date == null) { date = chars; }


             }

            public Bitmap getImage() { return image; }
            public String getTitle() { return title; }
            public StringBuffer getDescription() { return description; }
            public String getDate() { return date; }


    }

        private void resetDisplay (String title, String date, Bitmap image, StringBuffer description) {

            TextView titleView = (TextView) findViewById (R.id.imageTitle);
            titleView.setText(title);

            TextView dateView = (TextView) findViewById(R.id.imageDate);
            dateView.setText(date);

            ImageView imageView = (ImageView) findViewById (R.id.imageView1);
            imageView.setImageBitmap(image);

            TextView descriptionView = (TextView) findViewById (R.id.imageDescription);
            descriptionView.setText(description);
        }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.activity_nasa_daily_image, menu);
        return true;
    }

}

What is the expected output? What do you see instead?

EMULATOR SCREEN IS BLANK

What version of the product are you using? On what operating system?

i am using eclipse version 21.0.1 on windows7 

Please provide any additional information below.

Original issue reported on code.google.com by aaashu...@gmail.com on 17 Mar 2013 at 11:32

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions