我想在调用webservice时显示progressdialog,并在请求完成时停止对话框。 我做了以下方式,但是在Web服务请求完成后显示的对话框。
public class NetWorkRunTask extends AsyncTask<String, Void, String> {
Context ctx;
public NetWorkRunTask(Context ctx)
{
this.ctx=ctx;
mProgressDialog = new ProgressDialog(ctx);
}
ProgressDialog mProgressDialog;
@Override
protected void onPreExecute() {
mProgressDialog.setMessage("Please wait....");
mProgressDialog.setIndeterminate(true);
mProgressDialog.setCancelable(false);
mProgressDialog.show();
}
@Override
protected String doInBackground(String... params) {
//contactService.getAssetsAtFirstRun();
// mProgressDialog.show();
return ServerConnection.getXmlRespFromUrl(params[0]); //this will include HttpPost
//return null;
}
@Override
protected void onPostExecute(String result) {
if(mProgressDialog != null)
{
if(mProgressDialog.isShowing())
{
mProgressDialog.dismiss();
// uti.showToast(getBaseContext(), "Zapisano kontakty.");}
}
}
}
}
和onClickListener
String xml=null;
try {
xml =new NetWorkRunTask(MyActivity.this).execute(finalURL,null,null).get();
}catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
这里出了什么问题......
答案 0 :(得分:3)
只需在你的asynctask中执行此操作
@Override
protected void onPostExecute(String result) {
mProgressDialog.dismiss();
}
答案 1 :(得分:1)
在调用异步任务时删除get()方法 更改 xml = new NetWorkRunTask(MyActivity.this).execute(finalURL,null,null).get();
到
xml = new NetWorkRunTask()。execute(finalURL,null,null);
和
公共类NetWorkRunTask扩展了AsyncTask {
ProgressDialog mProgressDialog;
@Override
protected void onPreExecute() {
mProgressDialog = new ProgressDialog(MainActivity.this);
mProgressDialog.setMessage("Please wait....");
mProgressDialog.setIndeterminate(true);
mProgressDialog.setCancelable(false);
mProgressDialog.show();
}
@Override
protected String doInBackground(String... params) {
//contactService.getAssetsAtFirstRun();
return ServerConnection.getXmlRespFromUrl(params[0]); //this will include HttpPost
}
@Override
protected void onPostExecute(String result) {
if(mProgressDialog != null)
{
if(mProgressDialog.isShowing())
{
mProgressDialog.dismiss();
// uti.showToast(getBaseContext(), "Zapisano kontakty.");}
}
}
}
答案 2 :(得分:0)
在您的代码中,您调用'get()'方法。 [get() - 如果需要,等待计算完成“]。 用下面的代码替换你的Asynch任务调用行,然后尝试
xml = new NetWorkRunTask(MyActivity.this).execute(finalURL,null,null);
答案 3 :(得分:0)
您可以在Async任务中定义接口
public interface OnProcessCompleteListener{
public void onSuccess(String result);
public void onFailure();
}
在您的活动类中,您可以实现回调方法,并可以将结果返回给类。
OnProcessCompleteListener listener = listener = new OnProcessCompleteListener() {
@Override
public void onSuccess(String result) {
// do what u want
}
@Override
public void onFailure() {
}
};
将'listener'传递给AsyncTask并调用onSuccess(String result),onFailure()方法。
@Override
protected void onPostExecute(String result) {
if(result != null) {
listner.onSuccess(result);
}else{
listner.onFailure();
}
if(mProgressDialog != null){
if(mProgressDialog.isShowing()){
mProgressDialog.dismiss();
// uti.showToast(getBaseContext(), "Zapisano kontakty.");}
}
}
}