Reputation: 65
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
.
Upvotes: 0
Views: 108
Reputation: 886
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
Upvotes: 1
Reputation: 292
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
Upvotes: 0