【问题标题】:ProgressDialog not showing while making HTTP request发出 HTTP 请求时没有显示 ProgressDialog
【发布时间】:2013-08-12 01:18:04
【问题描述】:

我想在 http 连接请求时显示 ProgressDialog。

有请求方法。

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;
}

但是当这个方法运行时。 ProgressDialog 未显示。 如何解决这个问题?

【问题讨论】:

标签: android progressdialog


【解决方案1】:

您需要拨打dialog.show()

启动对话框并将其显示在屏幕上。窗口被放置在 应用层和不透明。请注意,您不应覆盖此 显示对话框时进行初始化的方法,而不是 在onStart() 中实现它。

另外,我建议你在AsyncTask class'doInBackground() 中执行此操作。
onPreExecute() 中显示ProgressDialog,在onPostExecute() 中将其关闭。

【讨论】:

  • 哥们,我绝对确定ProgressDialog.show 会调用dialog.show(),我在源代码中看到过
  • 如果我在AsyncTask中这样做,我不知道如何返回结果。我认为这个请求必须是同步的
  • 视情况而定。您可以将AsyncTask 设为内部类。
【解决方案2】:
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;
}

别忘了调用 dialog.dismiss();一个按钮

【讨论】:

    【解决方案3】:

    没有显示进度对话框的原因有很多,但在您的情况下,我想这是因为您传递了错误的Context 以显示ProgessDialog,请检查您的Activity 上下文。请确保您使用正确的Context,或者将其更改为ApplicationContext

    ProgressDialog dialog = ProgressDialog.show(activity, "", "Loading...",true);
    

    检查这一行,尤其是activity。希望这会有所帮助。

    【讨论】:

      猜你喜欢
      • 2013-11-30
      • 1970-01-01
      • 2017-01-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多