Mi solución utiliza el código de la respuesta aceptada sin embargo he encontrado que no funcionaría sin el uso de un AsyncTask
Aquí está mi código, primero recupero el XML y lo analizo en un método que usa una AsyncTask
public class RetrieveFeed extends AsyncTask {
URL url;
ArrayList<String> headlines = new ArrayList();
ArrayList<String> links = new ArrayList();
@Override
protected Object doInBackground(Object[] objects) {
// Initializing instance variables
try {
url = new URL("http://feeds.bbci.co.uk/news/rss.xml?edition=uk");
XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
factory.setNamespaceAware(false);
XmlPullParser xpp = factory.newPullParser();
// We will get the XML from an input stream
xpp.setInput(getInputStream(url), "UTF_8");
/* We will parse the XML content looking for the "<title>" tag which appears inside the "<item>" tag.
* However, we should take in consideration that the rss feed name also is enclosed in a "<title>" tag.
* As we know, every feed begins with these lines: "<channel><title>Feed_Name</title>...."
* so we should skip the "<title>" tag which is a child of "<channel>" tag,
* and take in consideration only "<title>" tag which is a child of "<item>"
*
* In order to achieve this, we will make use of a boolean variable.
*/
boolean insideItem = false;
// Returns the type of current event: START_TAG, END_TAG, etc..
int eventType = xpp.getEventType();
while (eventType != XmlPullParser.END_DOCUMENT) {
if (eventType == XmlPullParser.START_TAG) {
if (xpp.getName().equalsIgnoreCase("item")) {
insideItem = true;
} else if (xpp.getName().equalsIgnoreCase("title")) {
if (insideItem)
headlines.add(xpp.nextText()); //extract the headline
} else if (xpp.getName().equalsIgnoreCase("link")) {
if (insideItem)
links.add(xpp.nextText()); //extract the link of article
}
} else if (eventType == XmlPullParser.END_TAG && xpp.getName().equalsIgnoreCase("item")) {
insideItem = false;
}
eventType = xpp.next(); //move to next element
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (XmlPullParserException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return headlines;
}
public InputStream getInputStream(URL url) {
try {
return url.openConnection().getInputStream();
} catch (IOException e) {
return null;
}
}
public ArrayList<String> heads()
{
return headlines;
}
}
Luego, en la actividad principal:
public class MainActivity extends ListActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ArrayList<String> headlines = new ArrayList<>();
RetrieveFeed getXML = new RetrieveFeed();
getXML.execute();
headlines = getXML.heads();
// Binding data
ArrayAdapter adapter = new ArrayAdapter(this,
android.R.layout.simple_list_item_1, headlines);
setListAdapter(adapter);
}
}
Asegúrate de que tienes el permiso usos correctos en su manifiesto, lo puso dentro de la etiqueta de la aplicación, pero fuera de cualquier etiqueta de actividad
<uses-permission android:name="android.permission.INTERNET"/>
y, finalmente, al extender ListActivity lo que ned asegurarse de que tiene un ListView en su diseño xml principal con el id:
android:id="@android:id/list"
No sé si funcionará, ya que no está utilizando un método de tarea asíncrono – Bachask8