windymonkey / headfirstandroid

Automatically exported from code.google.com/p/headfirstandroid
0 stars 0 forks source link

NasaDailyImage RSS feed #2

Open GoogleCodeExporter opened 8 years ago

GoogleCodeExporter commented 8 years ago
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

GoogleCodeExporter commented 8 years ago
Did you get this issue resolved? Am facing exact same problem.
Any help appreciated. Have spent 2 days on this code. Frustrating.

Original comment by 2000amol...@gmail.com on 18 May 2013 at 2:58

GoogleCodeExporter commented 8 years ago
I'm having this problem too, on chapter 3.  The error is 
NetworkOnMainThreadException.
There are lots of Google hits on how to fix this, but they all involve your 
class extending AsyncTask, which we can't do, as the classes we have are 
already extending other things..
Not sure what else to do.

Original comment by musicmog...@gmail.com on 10 Jun 2013 at 9:36

GoogleCodeExporter commented 8 years ago
i hv same problem,,,,pliz friends solve my problem, i  have so much expectation 
from this apps

Original comment by prasadde...@gmail.com on 15 Jul 2013 at 6:07

GoogleCodeExporter commented 8 years ago
I have same problem but the rss address on the book is wrong, the correct one 
is: "http://www.nasa.gov/rss/dyn/image_of_the_day.rss" there are some changes 
in the new xml from nasa, like there is a field called enclosure, where is 
image's url like this:
<enclosure 
url="http://www.nasa.gov/sites/default/files/styles/946xvariable_height/public/i
mage08012013_250m.jpg?itok=FFdZZjJv" length="5360210" type="image/jpeg" />

So the we need to tell the parse where to looking for. I haven't yet solved the 
problem but i'm figure out how to.

Original comment by 149091...@unifacs.edu.br on 4 Aug 2013 at 7:43

GoogleCodeExporter commented 8 years ago
But atleast title should be there na........there is nothing on screen

Original comment by puneetme...@gmail.com on 8 Aug 2013 at 10:59

GoogleCodeExporter commented 8 years ago
I am also getting a blank screen on my emulator .....Is dre ny1 who have 
resolved ds issue..If Resolved plzzz Assist..

Original comment by hunter.l...@gmail.com on 23 Aug 2013 at 6:35

GoogleCodeExporter commented 8 years ago
I have managed to get the url from the <enclosure>-tag, but my knowwledge of 
Android is rather shallow - how do I get image to show up??

Part of my code in startElement:

if (localName.equals("enclosure")) {
                String attsValue = attributes.getValue("url");
                inUrl = true;
            } else {
                inUrl = false;
            }

Dont know what to do with attsValue in Head First code.....

Original comment by thomas.d...@gmail.com on 16 Sep 2013 at 11:43

GoogleCodeExporter commented 8 years ago
Did anybody solve this ? Coz I'm also getting a blank screen and no errors. 
Eclipse kepler on windows 7

Original comment by Mlingo...@gmail.com on 23 Dec 2013 at 8:45

GoogleCodeExporter commented 8 years ago
I have spotted the point where it gets run time exception. It is in the 
definition of method processFeed(). But I was not able to resolve the issue.

Original comment by bksumit1...@gmail.com on 25 Apr 2014 at 9:07