进度诊断未更新

我一直在使用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();


烙印99
浏览 115回答 2
2回答

达令说

根据安卓文档:“onProgressUpdate(Progress...),在调用 publishProgress(Progress...) 后在 UI 线程上调用。执行时间未定义。此方法用于在后台计算仍在执行时在用户界面中显示任何形式的进度。例如,它可用于对进度条进行动画处理或在文本字段中显示日志”使用它

暮色呼如

您必须发布进度,然后只有 具有适当的值。Integer... values像这样:@Overrideprotected String doInBackground(Context... params) {&nbsp; &nbsp; //Part-1 of the task done&nbsp; &nbsp; publishProgress(20);&nbsp; &nbsp; //Part-2 of the task done&nbsp; &nbsp; publishProgress(50);&nbsp; &nbsp; //Part-3 of the task done&nbsp; &nbsp; publishProgress(100);&nbsp; &nbsp; return “success”;}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java