Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android AsyncTask not calling onPostExecute

When I call my AsyncTask, doInBackground is run but onPostExecute is never called afterwards. Eclipse is not complaining about @Overrides or any of the parameters in the AsyncTask, so I'm not sure what the issue is. Can anyone tell me what the problem is?

Calling AsyncTask:

new AsyncDataFetcher(this).execute(productInfoUrls);

productInfoUrls is a list of urls.

AsyncTask:

private static class AsyncDataFetcher extends AsyncTask<List<String>, Void, List<JSONObject>> {

    Context context;

    public AsyncDataFetcher(Context context) {
        this.context = context;
    }

    @Override
    protected List<JSONObject> doInBackground(List<String>... urls) {
        List<JSONObject> jsonList = new ArrayList<JSONObject>();
        JSONParser json_parse = new JSONParser();
        for (int i = 0; i < urls[0].size(); i ++) {
            jsonList.add(json_parse.getJSONFromUrl(urls[0].get(i)));
        }
        return jsonList;
    }

    @Override
    protected void onPostExecute(List<JSONObject> jsonList) {
        if(jsonList != null){
            productInfoParser.Parse(jsonList); 
            loaded = true;
        }
    }
}
like image 310
Corey Wu Avatar asked Nov 01 '22 16:11

Corey Wu


1 Answers

Everything looks appropriate in your async task structure, but what you are doing in doInBackground could take forever! I suspect that your app is starting the doInBackground and never finishing it.

I would add some debug logs to see how the json getting is progressing.

@Override
protected List<JSONObject> doInBackground(List<String>... urls) {
    List<JSONObject> jsonList = new ArrayList<JSONObject>();
    JSONParser json_parse = new JSONParser();
    for (int i = 0; i < urls[0].size(); i ++) {
        jsonList.add(json_parse.getJSONFromUrl(urls[0].get(i)));
        Log.d(TAG, "added " + i);
    }
    Log.d(TAG, "returning jsonList");
    return jsonList;
}
like image 190
HalR Avatar answered Nov 09 '22 05:11

HalR