Joe
Joe

Reputation: 3581

ProgressDialog not showing while making HTTP request

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?

Upvotes: 3

Views: 4703

Answers (3)

An SO User
An SO User

Reputation: 25008

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.

Upvotes: 2

user2652394
user2652394

Reputation: 1686

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.

Upvotes: 0

Armanoide
Armanoide

Reputation: 1344

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

Upvotes: 0

Related Questions