Reputation: 4408
I have a progress dialog class that is a subclass of my main activity. Which in itself runs fine.
public class UpdateFeedTask extends AsyncTask<Void, Void, Void> {
ProgressDialog loading;
@Override
protected void onPreExecute() {
loading = ProgressDialog.show(NewsFeedActivity.this,"Please wait...", "Retrieving data ...", true);
}
@Override
protected void onProgressUpdate(Void... progress) {
}
@Override
protected void onPostExecute(Void result) {
loading.dismiss();
}
@Override
protected Void doInBackground(Void... params) {
// TODO Auto-generated method stub
return null;
}
}
I have a method called getNews which sends a http post request and parses the result, this takes a few seconds. In this method I call the UpdateFeedTask to show the progress dialog asynchronously, but it doesn't appear to do it simultaneously. The progress dialog seems to open only after the http request is completed so instead of showing the progress dialog and sending the request it appears to complete the method getNews first and then briefly shows the progress dialog after for a split second. How can I make the dialog show before I fetch the data? Here is how I call the progress dialog
public void getNews(){
//show the progress dialog
new UpdateFeedTask().execute();
//go fetch some data
WheruClient newsFeedLocation = new WheruClient();
try {
newsFeedArray = newsFeedLocation.getLocationActivity(sessionKey,page,filter,when);
}catch (ClientProtocolException e) {
Toast.makeText(getApplicationContext(),e.getMessage(), Toast.LENGTH_LONG).show();
} catch (IOException e) {
Toast.makeText(getApplicationContext(),e.getMessage(), Toast.LENGTH_LONG).show();
}catch (JSONException e) {
Toast.makeText(getApplicationContext(),e.getMessage(), Toast.LENGTH_LONG).show();
}
}
**EDIT new async class sends http request in background and gets back json array, but I get a fatal error that reads
04-07 15:33:02.967: ERROR/AndroidRuntime(11004): Caused by: java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
I read your examples and I think im missing something about doing work in the background, any thoughts?
public class UpdateFeedTask extends AsyncTask<Void, Void, JSONArray> {
@Override
protected void onPreExecute() {
//loading = ProgressDialog.show(NewsFeedActivity.this,"Please wait...", "Retrieving data ...", true);
}
@Override
protected void onProgressUpdate(Void... progress) {
}
@Override
protected void onPostExecute(JSONArray result) {
//loading.dismiss();
updateFeed(result);
}
@Override
protected JSONArray doInBackground(Void... params) {
WheruClient newsFeedLocation = new WheruClient();
try {
newsFeedArray = newsFeedLocation.getLocationActivity(sessionKey,page,filter,when);
} 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 newsFeedArray;
}
}
Upvotes: 0
Views: 2363