执行异步任务时未显示Android进度对话框

时间:2016-01-09 09:16:24

标签: android android-asynctask progressdialog

我正在尝试使用asynctask进行API调用时显示进度对话框。问题是,除非API调用已完成,否则ProgressDialog不会出现。

以下是异步任务代码:

public class GetAPIResponse extends AsyncTask<APIRequestModel, Void, String> {

/**
 * Global instance of the HTTP transport.
 */
private final HttpTransport HTTP_TRANSPORT = new NetHttpTransport();
Gson gson = new Gson();
public AsyncResponse delegate = null;
Context context;

private ProgressDialog mLoadingDialog;
private Handler mHandler = new Handler();

private void showLoadingDialog(final String title, final String msg) {
    mHandler.post(new Runnable() {
        @Override
        public void run() {
            if (mLoadingDialog == null) {
                mLoadingDialog = ProgressDialog.show(context, title, msg);
            }
            mLoadingDialog.setTitle(title);
            mLoadingDialog.setMessage(msg);
        }
    });
}

private void hideLoadingDialog() {
    mHandler.post(new Runnable() { //Make sure it happens in sequence after showLoadingDialog
        @Override
        public void run() {
            if (mLoadingDialog != null) {
                mLoadingDialog.dismiss();
            }
        }
    });
}

public GetAPIResponse(Context context)
{
    this.context = context;
}

@Override
protected void onPreExecute() {
    super.onPreExecute();
    showLoadingDialog("", "Creating your account");      
}

@Override
protected void onPostExecute(String result) {
   super.onPostExecute(result);
   hideLoadingDialog();            
}

@Override
protected String doInBackground(APIRequestModel... params) {
    String result = "";
    try {

        HttpRequestFactory httpRequestFactory = createRequestFactory(HTTP_TRANSPORT);
        HttpRequest request = httpRequestFactory
                .buildGetRequest(new GenericUrl(params[0].RequestUrl));

        request.getUrl().put("json", gson.toJson(params[0].KeyValuePair));
        result = request.execute().parseAsString();
    } catch (Exception e) {
        Log.e("GetAPIResponse:", e.getMessage());
    }
    return result;
}

public HttpRequestFactory createRequestFactory(
        final HttpTransport transport) {
    return transport.createRequestFactory(new HttpRequestInitializer() {
        public void initialize(HttpRequest request) {
            GoogleHeaders headers = new GoogleHeaders();
            request.setHeaders(headers);
            JsonHttpParser parser = new JsonHttpParser(new JacksonFactory());
            request.addParser(parser);
        }
    });
}
}
谢谢。

4 个答案:

答案 0 :(得分:0)

更新并尝试 GetAPIResponse

中的代码
private ProgressDialog pd;

public GetAPIResponse(Context context)
{
    this.context = context;
    pd = new ProgressDialog(context);
}

private void showLoadingDialog(final String title, final String msg) {
    pd.setMessage(msg);
    pd.show();
    pd.setCancelable(false);
    pd.setCanceledOnTouchOutside(false);
}

private void hideLoadingDialog() {
    pd.hide();
    pd.dismiss();
}

将此asyncTask称为以下::

new GetAPIResponse(context).execute(apiRequestModel);

希望您的其他代码正常运行。

答案 1 :(得分:0)

if (mLoadingDialog == null) {
       mLoadingDialog = ProgressDialog.show(context, title, msg);
}
mLoadingDialog.setTitle(title);
mLoadingDialog.setMessage(msg);

这是上下文的问题,你没有得到上下文所以用

替换它
if (mLoadingDialog == null) {
       mLoadingDialog = ProgressDialog(Activityname.this);
}
mLoadingDialog.setTitle(title);
mLoadingDialog.setMessage(msg);
mLoadingDialog.show();

答案 2 :(得分:0)

一个简单但可能不是最佳的解决方案是在执行异步任务之前在onCreate中启动进度对话框,并从异步任务中删除onPreExecute()方法。同时将进度初始化转移到异步任务之外。

'0'

我在doInBackground()中添加了一个可运行5秒的runnable,而不是你拥有的http代码,它对我来说很好。如果您遇到任何问题,请告诉我。

答案 3 :(得分:0)

首先提一下那些事情, Asynctask 如何在实际工作!!

这是一个简单的例子,

class MyTask extends AsyncTask<Void, Void, Void> {
 ProgressDialog pd;
    @Override
    protected void onPreExecute() {
      super.onPreExecute();
       pd = new ProgressDialog(MainActivity.this);
       pd.setMessage("loading");
       pd.show();
    }

    @Override
    protected Void doInBackground(Void... params) {
      // Do your request
    }

    @Override
    protected void onPostExecute(Void result) {
      super.onPostExecute(result);
      if (pd != null)
      {
         pd.dismiss();
      }
    }
  }