osm
osm

Reputation: 622

AsycTask Doesn't Execute

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?

Upvotes: 1

Views: 84

Answers (1)

TootsieRockNRoll
TootsieRockNRoll

Reputation: 3288

if your doInBackground() method is not called, try calling your task like this, this is a known issue:

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB)
    new GetRoutes().executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, "test");
else
    new GetRoutes().execute("test");

and add this to the top of the method that calls the asynk task, (eclipse will ask you to so just in case ..)

@SuppressLint("NewApi")

SOLUTION UPDATE:

as I saw you are implementing the adapter right after the call of the asynktask, but with this beeing asynchronous, you MUST wait for the adapter until the task has finished, generally this is beeing made on the onPostExecute() method, so replace your lines

final ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
                    R.layout.list_item, android.R.id.text1, values);

list.setAdapter(adapter);

inside onPostExecute(), and retry

Upvotes: 2

Related Questions