android - 发出 HTTP 请求时未显示 ProgressDialog

标签 android progressdialog

我想在 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 未显示。 如何解决这个问题?

最佳答案

你需要调用dialog.show()

Start the dialog and display it on screen. The window is placed in the application layer and opaque. Note that you should not override this method to do initialization when the dialog is shown, instead implement that in onStart().

此外,我建议您在 AsyncTask 类的 doInBackground() 中执行此操作。
onPreExecute() 中,显示 ProgressDialog 并在 onPostExecute() 中关闭它。

关于android - 发出 HTTP 请求时未显示 ProgressDialog,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18178340/

相关文章:

android - 为同一设备获取不同的 ID。为什么?

android - 通过 monkeyrunner 脚本在 android 上使用 dd 创建虚拟文件

javascript - Cordova:暂停事件未触发

android - ProgressDialog - 无法解析符号 `ProgressDialog`

android - 错误选项卡中的 progressDialog

android - 获取ArtifactoryPublication的BuildType版本名称

Android 数据库列表适配器 : multiple columns, 对齐?

java - 在加载其他 Activity 时显示 ProgressDialog

android - ProgressDialog 在 AsyncTask 中显示错误

IOS 进度对话框