我一直在使用AsyncTask下载某个文件,并经历了一些教程,只是未能使进度条随着下载而移动。代码是和 AsyncTask 调用一个方法来执行 HTTP 连接,然后返回以正确的方式对数据进行排序,以便为应用程序操作它这是我的AsynTask,在主要活动 private class getFood extends AsyncTask<Void, Integer, Cursor> { private ProgressDialog mProgressDialog; @Override protected Cursor doInBackground(Void... params) { // Create URL object String site = "https://afternoon-ridge-50060.herokuapp.com/allsnacks"; URL url = createUrl(site); // Perform HTTP request to the URL and receive a JSON response back String jsonResponse = null; try { String jsonResponseEmpty = ""; // If the URL is null, then return early. if (url == null) { jsonResponse = jsonResponseEmpty; } HttpURLConnection urlConnection = null; InputStream inputStream = null; try { assert url != null; urlConnection = (HttpURLConnection) url.openConnection(); urlConnection.setReadTimeout(20000 /* milliseconds */); urlConnection.setConnectTimeout(25000 /* milliseconds */); urlConnection.setRequestMethod("GET"); urlConnection.setRequestProperty("Authorization", "\"token\": " + token); urlConnection.connect();
2 回答
达令说
TA贡献1821条经验 获得超6个赞
根据安卓文档:
“onProgressUpdate(Progress...),在调用 publishProgress(Progress...) 后在 UI 线程上调用。执行时间未定义。此方法用于在后台计算仍在执行时在用户界面中显示任何形式的进度。例如,它可用于对进度条进行动画处理或在文本字段中显示日志”
使用它
暮色呼如
TA贡献1853条经验 获得超9个赞
您必须发布进度,然后只有 具有适当的值。Integer... values
像这样:
@Override
protected String doInBackground(Context... params) {
//Part-1 of the task done
publishProgress(20);
//Part-2 of the task done
publishProgress(50);
//Part-3 of the task done
publishProgress(100);
return “success”;
}
添加回答
举报
0/150
提交
取消