3

I want to show ProgressDialog when http connection request.

there is request method.

protected Result request(String urlStr, String postData) {
    ProgressDialog dialog = ProgressDialog.show(activity, "", "Loading...",true);
    Result result = new Result();
    String message = "";
    try {
        message = HttpRequest.postURL(urlStr, postData);
        result = new Result(message);
    } catch (Exception e) {
        Log.e(TAG,"Failed to request data from " + urlStr + "\n" + e.getMessage());
    }
    dialog.dismiss();
    return result;
}

but when this method running. the ProgressDialog not showing. how to solve this problem?

Sufian
  • 6,405
  • 16
  • 66
  • 120
Joe
  • 3,581
  • 4
  • 23
  • 28
  • Possible duplicate of [ProgressDialog not showing while performing a task](http://stackoverflow.com/questions/12897205/progressdialog-not-showing-while-performing-a-task) – Sufian Jul 24 '16 at 11:12

3 Answers3

2

You need to call dialog.show()

Start the dialog and display it on screen. The window is placed in the application layer and opaque. Note that you should not override this method to do initialization when the dialog is shown, instead implement that in onStart().

Also, what I do suggest is that you do this in AsyncTask class' doInBackground().
In the onPreExecute(), display the ProgressDialog and in the onPostExecute() dismiss it.

Sufian
  • 6,405
  • 16
  • 66
  • 120
An SO User
  • 24,612
  • 35
  • 133
  • 221
  • buddy, i'm absolutely sure `ProgressDialog.show` will call `dialog.show()`, i been see it in source code – Joe Aug 12 '13 at 01:52
  • and i can't figure out how to return result if i do this in `AsyncTask`.i think this request must be synchronous – Joe Aug 12 '13 at 02:19
  • It depends. You can make `AsyncTask` as an inner class. – An SO User Aug 12 '13 at 03:28
0
protected Result request(String urlStr, String postData) {
    ProgressDialog dialog = ProgressDialog.show(activity, "", "Loading...",true);
    Result result = new Result();
    String message = "";
    try {
        message = HttpRequest.postURL(urlStr, postData);
        result = new Result(message);
    } catch (Exception e) {
        Log.e(TAG,"Failed to request data from " + urlStr + "\n" + e.getMessage());
    }
    dialog.show();
    return result;
}

don't forget to call dialog.dismiss(); with a button

Armanoide
  • 1,248
  • 15
  • 31
0

There are many reasons for the progress dialog not showing, but in your case, i guess it's because you passing the wrong Context to show the ProgessDialog, please check your Activity context. Make sure you use proper Contextfor that or just change it to ApplicationContext

ProgressDialog dialog = ProgressDialog.show(activity, "", "Loading...",true);

Check this line, especially the activity. Hope this helps.

user2652394
  • 1,686
  • 1
  • 13
  • 15