Retrieving data from the Internet in the background and displaying a ProgressDialog or ProgressBar

I am developing an application that requires access to a website for data and will show what data is on the device. I want to get data from the Internet in the background and show ProgressDialog or ProgressBar to devices, and when the application receives a response from the server application, open a dialog box or panel and show the data.

For this I use AsyncTask -

The 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();

// execute the operation with the response

but ProgressDialog is not displayed. (I think the reason is that this thread is not complete and the android is not valid only when the thread is completed).

- 1- ? , ? 2- ?

+3
2

- , "" , dialog.show() ​​ , onPostExecute().

+1

, ProgressDialog, :

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

new GetTask(this).execute();

. , ProgressDialog, , , . Managed Dialogs .

+2

Source: https://habr.com/ru/post/1729395/


All Articles