I got problems when I try to get info from XML to a string array. Is works fine if I only have one if statment withinf the: if (eventType == XmlPullParser.START_TAG) {...}
The thing is tho, I want to diffrent things to my array, both "Allergen" and "Artikelbenamning". Not sure if the problem is in the doInBackground or in the onPostExecute.
Any suggestions on what i've done wrong? The app dosen't crash it just never present the data.
Code:
protected ArrayList<String> doInBackground(String[] params) {
//holds info a 5 titles from xml rss file
ArrayList<String>titles=new ArrayList<>();
try {
//URL to rss feed to read
//params[0]=("http://www.aftonbladet.se/rss.xml";
URL url = new URL(params[0]);
//This class is used to create implementations of XML Pull Parser
XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
//Specifies that the parser produced by this factory will provide support for XML namespaces. By default the value of this is set to false.
factory.setNamespaceAware(true);
//XMLPull Parser is an interface that defines parsing functionality provided
//newPullparser()=Creates a new instance of a XML Pull Parser using the currently configured factory features.
XmlPullParser xpp = factory.newPullParser();
//Sets the input stream the parser is going to process. UTF-8 encodit that the RSS feed uses
xpp.setInput(url.openCoection().getInputStream(), "UTF_8");
//Retus the type of the current event (START_TAG, END_TAG, TEXT, etc.)
int eventType = xpp.getEventType();
//I want five titles, since the first two only contains info about the newspaper
//I set in the if statement less then 7 to get 5
while (eventType != XmlPullParser.END_DOCUMENT) {
// Looking for a start tag
if (eventType == XmlPullParser.START_TAG) {
//We look for "Artikelbenamning" tag in XML response
//getName()=For START_TAG or END_TAG events, the (local) name of the current element is retued
if (xpp.getName().equalsIgnoreCase("Allergen")) {
titles.add(xpp.nextText());
allergen=xpp.nextText();
}
if (xpp.getName().equalsIgnoreCase("Artikelbenamning")) {
titles.add(xpp.nextText());
namn=xpp.nextText();
}
}
//get next parsing event
eventType = xpp.next();
}//end while
}//end try
catch(MalformedURLException exp){
}
catch(IOException exp){
}
catch(XmlPullParserException exp){
}
//retu the arraylist with titles data to onPostExecute
retu titles;//builder.toString();
}
protected void onPostExecute(ArrayList<String> titleResult){
//get the listview
ListView listView=(ListView)findViewById(R.id.listView);
//create an adapter with data from titleResults arraylist that was filled from rss file
//in doInBackground
ArrayAdapter<String> adapter=new ArrayAdapter<String>(MainActivity.this,android.R.layout.simple_list_item_1,titleResult);
//set the adapter for the listview
listView.setAdapter(adapter);
//Whether the dialog is currently showing.
if(dialog.isShowing()){
//dismiss this dialog, removing it from the screen.
dialog.dismiss();
}
}
