I have come across many posts regarding the issue of AsycTask not running and wanted to confirm if that is indeed the issue with my implementation. Should I try to implement this method instead?
I call the following method in my onCreate method using new GetRoutes().execute("test");
. The information provided by the AsyncTask is used to build a ListView of parsed information. However, this method does not update the "values" array as it should.
private class GetRoutes extends AsyncTask<String, Void, String[]> {
@Override
protected String[] doInBackground(String... urls) {
List<String> in = new ArrayList<String>();
try{
RouteReader route = new RouteReader();
in = route.parse(new URL(RouteReader.URI).openStream());
} catch(IOException iox){
in.add("Major poopoo");
//in.add(getResources().getString(R.string.loading_error));
} catch(ArrayIndexOutOfBoundsException aiob){
in.add("Minor poopoo");
//in.add(getResources().getString(R.string.loading_error));
} catch(NullPointerException npe){
in.add("Small poopoo");
//in.add(getResources().getString(R.string.loading_error));
} catch (XmlPullParserException e) {
in.add(getResources().getString(R.string.loading_error));
}
return in.toArray(new String[in.size()]);
}
@Override
protected void onPostExecute(String[] result) {
values = result;
}
}
The RouteReader class is as follows:
public class RouteReader {
private XmlPullParser parser;
public final static String URI =
"http://webservices.nextbus.com/service/publicXMLFeed?command=routeList&a=ttc";
public RouteReader(){
parser = Xml.newPullParser();
}
public List<String> parse(InputStream in) throws XmlPullParserException, IOException {
try {
parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, false);
parser.setInput(in, null);
parser.nextTag();
return readFeed(parser);
} finally {
in.close();
}
}
private List<String> readFeed(XmlPullParser parser) throws XmlPullParserException, IOException {
List<String> entries = new ArrayList<String>();
parser.require(XmlPullParser.START_TAG, null, "body");
while (parser.next() != XmlPullParser.END_TAG) {
if (parser.getEventType() != XmlPullParser.START_TAG) {
continue;
}
String name = parser.getName();
if (name.equals("route")) {
entries.add(parser.getAttributeValue(null, "id"));
System.out.println(parser.getAttributeValue(null, "id"));
}
}
return entries;
}
}
Could you please help me in pinpointing the issue with this implementation?