0

I have an AsyncTask class which parse json data and inserts into an object, when I call it in normal activity classes it shows that data have been parsed but when I execute this on a class which extends Application then it gives zero result. Here is my class

public class AppGlobalData extends Application {
    public ArrayList<YoutubeItem> gotItem= new ArrayList<YoutubeItem>();
    private YouTubeParser parser;

    @Override
    public void onCreate() {
        // TODO Auto-generated method stub
        super.onCreate();


        new ParserLoader().execute();
    }

    public class ParserLoader extends AsyncTask<Void, Void, Void> {

        @Override
        protected Void doInBackground(Void... params) {
            Log.i("Succeed?", "Yes");
            parser = new YouTubeParser(
                    "http://powergroupbd.com/youtube/getyoutubejson.php");
            try {
                gotItem = parser.parseInitiator();
            } catch (ClientProtocolException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (JSONException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }


            return null;
        }

    }

    public ArrayList<YoutubeItem> getGotItem() {
        return gotItem;
    }

    public void setGotItem(ArrayList<YoutubeItem> gotItem) {
        this.gotItem = gotItem;
    }

}

I cant figure out the problem, can anyone help? please be noted that this class runs and logs my String but doesnt parse data.

Reyjohn
  • 2,654
  • 9
  • 37
  • 63

1 Answers1

2

I think what might happen is that when new ParserLoader().execute(); it doing the work asynchronously. When your Activity load and call the getGotItem() the Asynctask might not have finished

Erik
  • 5,039
  • 10
  • 63
  • 119
  • I dont need the postexecute method here, I just need the class do in background. – Reyjohn Sep 12 '13 at 20:48
  • I didnt downvote but I up voted, anywys I have tried with asynctask before those worked, but when it's about json parsing it is not working – Reyjohn Sep 12 '13 at 20:53
  • I just want the arraylist to be loaded in that class, thats it :) – Reyjohn Sep 12 '13 at 21:01
  • 1
    I was a bit hasty because of the few beers they forced me to drink sorry. I think what might happen is that when `new ParserLoader().execute();` it doing the work asynchronously. When your Activity load and call the `getGotItem()` the Asynctask might not have finished – Erik Sep 12 '13 at 23:28
  • is there any way to load the activity after a while that the application class could finish the background process? – Reyjohn Sep 13 '13 at 08:57
  • The easiest way I can think of now is that you move the `new ParserLoader().execute();` to a `public void startGotItem()` and from your Activity just start the process and wait for the result in an `AsyncTask Void doInBackground`. Or have your Activity from the `AsyncTask Void doInBackground` loop polling for the `getGotItem()` – Erik Sep 13 '13 at 10:08
  • Then If I want to use that GotItem in another class do I need to run that background process again? If yes that what is beneficiary here using application class? – Reyjohn Sep 13 '13 at 19:54
  • I would say yes this is perfectly alright. You can call `setGotItem()` and `getGotItem()` from anywhere just make sure you synchronize since the `gotItem` is a public field. Other ways to do this is to use the `Service` class that you [Bind](http://developer.android.com/guide/components/bound-services.html) to – Erik Sep 13 '13 at 20:12
  • Ask a new question about this, show your code in the question again. This question is Answered i think. Please Accept so we can close this question – Erik Sep 13 '13 at 20:22
  • can you provide me a complete code how to do this? I am unable to find out the exact thing I want, sorry for that – Reyjohn Sep 13 '13 at 20:23
  • here is my new question http://stackoverflow.com/questions/18794995/activity-class-being-called-before-the-asynctask-finishes-background-process-in – Reyjohn Sep 13 '13 at 20:49