2

I have a class which extends Application class to load some data before any activity launches. I did some json parsing there, but the problem is the activity class is being called before the doInBackground finishes in the application class, thats why first time I get a variable without any value in it. Here is my code, I need a solution of it, plz help!

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

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

        parser = new YouTubeParser(
                "http://powergroupbd.com/youtube/getyoutubejson.php");
        new ParserLoader().execute();

    }

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

        @Override
        protected Void doInBackground(Void... params) {

            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;
    }

}

another point is, If I parse the data in onCreate it works in the lower version of android but in ICS it's causing network main thread exception.

Reyjohn
  • 2,654
  • 9
  • 37
  • 63
  • [See this answer](http://stackoverflow.com/questions/18517400/inner-class-can-access-but-not-update-values-asynctask/18517648#18517648) to create an interface to let the `Activity` know when the `AsyncTask` is done – codeMagic Sep 13 '13 at 20:51
  • The network main thread exception could be that onCreat() is blocking. onCreate() is only for creation not for work. Im in the pub again man, see you soon – Erik Sep 13 '13 at 21:20
  • I know about that exception, @codeMagic's link was usefull but I am stuck in a place where I am getting nullpointer exception. please help – Reyjohn Sep 13 '13 at 21:22
  • What @Mikael Olsson is saying is creat an asynctask in your activety that will do what onCreate is doing – Erik Sep 13 '13 at 21:29
  • I need to create that asynctask in the application class because I have to use that variable globally in seperated classes, thats why I am using the Application class – Reyjohn Sep 13 '13 at 21:36
  • You have created a content provider that resist in the Application class. Everyone that gona try to harvest from this provider need to be pationt and listen for it, does it have some content to provide?. Create AsyncTaks in your listening Activitys and adjust to whats happening, Avtivity will never know when data is accessable, spin around a loop and wait – Erik Sep 13 '13 at 21:48
  • Where are you getting the `NPE`? – codeMagic Sep 13 '13 at 22:02
  • maybe post your Activity code Reyjohn, yea we need to see what your doing (back from the pub) – Erik Sep 13 '13 at 22:12

1 Answers1

-1

It's not good practice to start an activity and try to hold the onCreate event until data has been processed or received. Instead override the onPostExecute method in the AsyncTask which will be executed after doInBackground finish.

protected void onPostExecute(Void v) {
     // Add your code here !!!
 }

The reason you get a main thread exception on ICS is because you are running some network API (YouTubeParser) on the main UI thread.

Try this instead.

Thread t = new Thread(new Runnable() {
 public void run() {
       parser = new YouTubeParser(
            "http://powergroupbd.com/youtube/getyoutubejson.php");
    new ParserLoader().execute();
 }
}
t.start();
Mikael Olsson
  • 315
  • 3
  • 7
  • I know that, please be notet that I want to parse all the json data before any activity class is launched, because I am using the parsed value in those activities, thanks – Reyjohn Sep 13 '13 at 21:27
  • You gona have to construct your activity in a way so it wait, listen for Application to finish, hense the AsyncTask in the activity. – Erik Sep 13 '13 at 21:32
  • I did everything like this, but I am getting nullpointer exception http://stackoverflow.com/questions/12575068/how-to-get-the-result-of-onpostexecute-to-main-activity-because-asynctask-is-a – Reyjohn Sep 13 '13 at 21:37
  • 1
    If you try to wait for the Json data to be parsed before allowing onCreate to finish, then you halt the main thread. Eventually Android OS will detect that and tell the user that the app has stopped. Let onCreate setup and start the AsyncTask AND add a progress dialog which is visible while AsyncTask is working. – Mikael Olsson Sep 14 '13 at 07:49
  • Reyjohn it's hard to figure out why you get a nullpointer exception. Can you please copy & paste the exception message from Logcat. – Mikael Olsson Sep 14 '13 at 07:56
  • 2
    Don't use AsyncTask from within the Application Context. AsyncTask is meant to be used in activities or fragments. If you really want your YouTubeParser to finish before continuing your app, why not make an activity that just shows a splash and a progress indicator, while your AsyncTask is running in that activity. Then when onPostExecute is executed you finish the activity and start the real main activity. – Mikael Olsson Sep 14 '13 at 08:31
  • This is actually a good answer, and all the comments points out what is needed to be done. Why not make what you need to store from the response into one of the following: singleton, serializable class (put in the bundle), sharedpreferences or an sqlite database. – Joakim Engstrom Sep 16 '13 at 13:51