0

I'm doing an application that reads a xml from a url and displays it on screen, but when I want to read the answer gives me a FileNotFoundException

this is the code:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    ListView rss = (ListView) findViewById(R.id.list);
    try {

       Authenticator.setDefault(new Authenticator() {
            @Override
            protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication("acceso!a!backend", "nu3v0!1nf0b43!2013".toCharArray());
            }
        });


        URL rssUrl = new URL("http://ec2-54-224-94-185.compute-1.amazonaws.com/adjuntos/162/rss/home_mobile.xmlx");

        SAXParserFactory factory = SAXParserFactory.newInstance();
        SAXParser saxParser = factory.newSAXParser();
        XMLReader xmlReader = saxParser.getXMLReader();
        RSSHandler rssHandler = new RSSHandler();
        xmlReader.setContentHandler(rssHandler);

////////here I am getting the FileNotFoundException

        InputSource inputSource = new InputSource(rssUrl.openStream());
        xmlReader.parse(inputSource);

        NoticiasAdapter na = new NoticiasAdapter(this ,rssHandler.getChannel());
        rss.setAdapter(na);

    } catch (IOException e) {
        e.printStackTrace();
    } catch (SAXException e) {
        e.printStackTrace();
    } catch (ParserConfigurationException e) {
        e.printStackTrace();
    }
}

3 Answers3

0

It's likely that URL does not exist, then. :)

Have you tried visiting that URL in your browser to confirm that it exists?

Also, the URLConnection throws the IOException when you start doing I/O because that's when it tries to make the physical connection. If you want the system to make the connection earlier (to make sure the URL exists, for example), you can use URLConnection's connect() method to make it connect earlier. (Just make sure you've done necessary connection configuration -0setting headers, etc. -- beforehand!) There's nothing wrong with waiting until you do I/O to make the connection, so you don't need to call connect() to make the connection earlier, but you can make the connection earlier if you want to.

sigpwned
  • 6,957
  • 5
  • 28
  • 48
0

I tried to open that url and it gives me a 404 error not found.

0

It seems likely the URL does not exist.

You could modify rssUrl.openStream() into two lines

  1. URLConnection connection = rssUrl.openConnection()
  2. InputSource inputSource = new InputSource(connection.getInputStream())

This way you could debug to figure out if you are able to connect instead of doing it all in one step. After all openStream() is shorthand for openConnection().getInputStream() http://docs.oracle.com/javase/7/docs/api/java/net/URL.html#openStream()

Once you figure out you have a valid URL and are able to connect you can change back to your implementation if desired.

kalelien
  • 432
  • 2
  • 10