tags:

views:

409

answers:

1

hi all,

I am developing an application which require accessing a website for data, and will show that data on device. I wants to fetch data from Internet in background and show ProgressDialog or ProgressBar on device and when application receive response from server app will dismiss the dialog or bar and will show data .

For this i am using AsyncTask -

code for AsyncTask is as follows--

ServerTask extends AsyncTask { @Override protected void onPreExecute() { dialogAccessingServer = new ProgressDialog(ctx); dialogAccessingServer.setMessage(shownOnProgressDialog); dialogAccessingSpurstone.show(); }

    @Override
    protected ServerResponse doInBackground(String... urlArray) {

            String urlString = urlArray[0];
            HttpResponse serverResponseObject = null;

            //finding HttpResponse

            return serverResponseObject;

    }//end of doInBackground

    @Override
    protected void onPostExecute(HttpResponse serverResponseObject){
            dialogAccessingSpurstone.dismiss();

    }

}

and calling this code as follows--

ServerTask serverTaskObject = new ServerTask(); serverTaskObject.execute(); HttpResponse response = serverTaskObject.get();

//performing operation on response

but ProgressDialog is not shown.(I guess the reason for it is the thread is not complete and android invalidate only when thread has completed).

My Questions -- 1- If my guess is right ? If yes then how I should implement it? 2- If there is any other better way to do this?

thanks

+1  A: 

Following is a template code that displays a ProgressDialog while a task is executing in background:

class GetTask extends AsyncTask<Object, Void, String>
    {
        Context mContext;
            ProgressDialog mDialog = null;

            GetPhotoFeedTask(Context context)
    {
        mContext = context;
    }

        @Override
        protected void onPreExecute()
        {
            super.onPreExecute();

             mDialog = new ProgressDialog(mContext);
             mDialog.setMessage("Please wait...");
             mDialog.show();
        }
                @Override
        protected String doInBackground(Object... params)
        {
                  // do stuff in background : fetch response
                }

                @Override
        protected void onPostExecute(String result)
        {
            super.onPostExecute(result);
            setProgressBarIndeterminateVisibility(false);
            // mDialog.dismiss();
                }
}

and you invoke it from your activity using new GetTask(this).execute() statement;

Note: Note that while displaying a ProgressDialog if the user switches the Orientation or causes event that ensues one, the code might break. It is advised to use Managed Dialogs for such cases.

Samuh
hi Samuh,Thanks for response.But this code is almost same, which I am using.My question is, if the activity from which I am calling GetTask(this).execute() has not completed, means it is waiting for response with this code-- response = serverTaskObject.get();then progress dialog does not appear.and I think the reason for it is that the ui thread (in which calling activity is running) has not completed and android invalidate only when the thread will complete.My Questions -- 1- If my guess is right ? If yes then how I should implement it? 2- If there is any other better way to do this?
Alok
I am not sure I understand your question; Presume in your onCreate() you have few stmts and one of them happens to be GetTask(this).execute(). onCreate() stmts runs on UI thread. AsyncTask creates a separate thread but onPreExecute() and onPostExecute() methods have handle to UI thread and so can update UI.JVM will execute statements in your onCreate and when it encounters a GetTask(this).execute() it will start a new thread and will continue with the next statement in onCreate(this depends on JVM no guarantees). Whenever, onPreExecute() is invoked, the ProgressDialog will be displayed.
Samuh
Take a look at the following:http://developer.android.com/resources/articles/painless-threading.html http://developer.android.com/reference/android/os/AsyncTask.html
Samuh
hi,to clarify my question, if in onCreate(), GetTask(this).execute() is the last statement than Progress Dialog appears on the screen, but if after this statement there are statements like response = GetTaskObject.get(); [this method of AsyncTask class waits for comleteion of doInBackground() and returns result.] and may be some other statements for dealing with response coming from server, Progress Dialog does not appear. And now the same assumption that android does not invalidate view until UI thread complete and ........now if my question is clear?thanks
Alok