I want to make an http request (I use ksoap2) in the onCreate()
during this time I want to have a ProgressDialog
on screen to block the user. Once the request is done, I want to finish my onCreate using the elements that I retrieve from my http request.
For now:
I have a class which implements Runnable. I instantiate it in my onCreate. This class make the http request, during this time a ProgressDialog is on the screen. Once it's done the ProgressDialog is dismissed. But since it's not in the UI thread the following of the onCreate is executed before getting the result of the http request.
I also tried, at first: in the onCreate I do a show()
of my ProgressDialog then the http request then a dismiss()
ProgressDialog and finally finish my onCreate with the result of my http request... it's ok except that the ProgressDialog is not showing this time.
Sorry for the messy explanations, my english is really not helping either.
Anyway, ideas ?
Thanks.
PS : BTW I know AsyncTask is better, it is what I usually use but having it with a ProgressDialog is not at all easy.
My question is more : is there a way in an onCreate()
to make an http request to get the value of an int
. While this request is made: showing a ProgressDialog
to block the user but also block the UI thread so that the end of the onCreate can use the value retrieved by the http request once it is complete.
An ugly way would be a loop in the onCreate with a sleep waiting for the value to be retrieved... that's really ugly.
Edit: I know you mentioned you didn't want to use AsyncTask
due to the complexities of showing the ProgressDialog
, but unless I'm missing something it should be as simple as the below. Alternatively, you could use a handler to fire the call back?
I think you need some kind of handler to callback once the HTTP
request is done, and then in the callback finish off your code.
I also use ksoap2
for a web service call and show a ProgressDialog
whilst it's called. Once the web server call is finished, it raises an event and I close the ProgressDialog
. I'm not sure if this will help you, but I have the following (of course, there may be better ways, but this works for me), this way the ProgressDialog
is called on the UI thread, an AsyncTask
completes and calls the complete method (which returns the thread back to the UI) and I dismiss the dialog:
// Notify user the web service is connecting
final ProgressDialog dialog = ProgressDialog.show(
Screen_GlobalLeaderboard.this, "Getting scores",
"Please wait...", true);
dialog.setCancelable(true);
WebService ws = new WebService(this) {
@Override
public void webServiceCallComplete(boolean success, Object data,
String errorMessage, WebMethods callingWebMethod) {
dialog.dismiss();
// Your complete code here
}
};
ws.getTopLeaderboardResults(top, gameMode);
Edit: I think what you're asking is onCreate
(ran from the UI thread), you want to show a ProgressDialog
and in the same method return a value from an HTTP request and once it completes, hide the dialog.
You then say you've tried that and the ProgressDialog
does not show. This is because you block the UI thread with the HTTP request.
Providing you call the HTTP request from the onCreate
method, you must run it in a separate thread (or as you done, implement Runnable
) and a handler which will call the runnable with which you can then handle the return value.
Put simply, you can't:
Show ProgressDialog
> Call HTTP request > Get value > Hide ProgressDialog
without adding another thread for the HTTP request (or the ProgressDialog
will not show) which in turn means your return HTTP request code must be outside of the calling onCreate
method.