showing progress bar during network call
I want to show the progress bar during web service call. I called progress bar before calling the service, but it is being called after the service call is finished and i have received the response.
ProgressDialog dialog = ProgressDialog.show(LogIn.this,"","Loading. Please wait...", true);
status=Loginvalid(method,username,psword); //calling the method for making service call
But, progress dialog is starting after the response is received from the service. Please how can i fix this problem..
public class Progress extends AsyncTask<String, Void, Void> {
protected void onPreExecute() {
ProgressDialog dialog = new MyProgressDialog(MyActivity.this, "Loading.. Wait..");
dialog.show();
}
@Override
protected Void doInBackground(String... params) {
// TODO Auto-generated method stub
// do your network connection
return null;
}
protected void onPostExecute(Void unused) {
dialog.dismiss();
}
}
Use AsyncTask
. It is the most effective and painless way of showing a progress dialog during a web service call.
show the progressbar on preexecute, call your webservice in doInBackground method, and dismiss the progressbar onPostexecute.
http://developer.android.com/reference/android/os/AsyncTask.html
In order to properly show the progress dialog it must be executed on UIThread, while all the other work(service call
) - in another. See the example here.
精彩评论