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.