0

I am writing an app to pull and display content from an atom feed. The method for that works, but I get the error "Unhandled exception: java.lang.Exception" when I try to call the method "showFeed()" in onNavigationItemSelected.

package com.myapp;
import java.net.URL;
import java.util.Iterator;
import com.sun.syndication.feed.synd.SyndEntry;
import com.sun.syndication.feed.synd.SyndFeed;
import com.sun.syndication.io.SyndFeedInput;
import com.sun.syndication.io.XmlReader;

// ... 

public boolean onNavigationItemSelected(@NonNull MenuItem item) {
    switch (item.getItemId()) {
        case R.id.navigation_home:
            showFeed();
    }
}

// ...

public void showFeed() throws Exception {

    URL url = new URL("http://feeds.feedburner.com/javatipsfeed");
    XmlReader xmlReader = null;

    try {

        xmlReader = new XmlReader(url);
        SyndFeed feeder = new SyndFeedInput().build(xmlReader);
        System.out.println("Title Value " + feeder.getAuthor());

        for (Iterator iterator = feeder.getEntries().iterator(); iterator.hasNext();) {
            SyndEntry syndEntry = (SyndEntry) iterator.next();
            System.out.println(syndEntry.getTitle());
        }
    } finally {
        if (xmlReader != null)
            xmlReader.close();
    }

}

I want to call this method in onNavigationItemSelected.

2
  • What is the exception? I guess it would be I/O exception try it write in seperate thread or use async task Commented Mar 25, 2019 at 10:23
  • 4
    Use a try-catch or add throws Exception to the method. Checked exceptions must be handled. Commented Mar 25, 2019 at 10:27

2 Answers 2

1

Your showFeed method is throwing Exception,

public void showFeed() throws Exception

This has to be caught using try-catch block or thrown further from the caller method which is onNavigationItemSelected

So either catch it like this,

 case R.id.navigation_home:
try{
            showFeed();
} catch (Exception e) {
e.printStackTrace();
}

Or throw it again like below,

public boolean onNavigationItemSelected(@NonNull MenuItem item) throws Exception
Sign up to request clarification or add additional context in comments.

1 Comment

This took away the error. Thanks. I marked this as answer because it showed an example
0

You are running network related threads on UI Thread which may cause ANR (Activity not responding) Follow the following tutorial.

Android AsyncTask example and explanation

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.