Android多文件分块上传的进度条

4

编辑:问题已解决,请往下滚动;)

根据这里的教程,我在AsyncTask中实现了一个进度条对话框来显示上传数据的百分比。目前它只显示从0%到100%,然后停留在100%直到所有数据上传完成。我正在尝试上传一个Json字符串和4张图片。

代码:

 private class UploaderTask extends AsyncTask<JSONArray, Integer, String> {

    private ProgressDialog dialog;
    protected Context mContext;
    long totalSize;

    public UploaderTask(Context context) {

        mContext = context;
    }

    @Override
    protected void onPreExecute() {

        dialog = new ProgressDialog(mContext);
        dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
        dialog.setTitle("Uploading data");
        dialog.setCancelable(false);
        //dialog.setIndeterminate(true);
        dialog.setMessage("Please wait...");
        dialog.show();
        //dialog.setProgress(0);

    }

    @Override
    public String doInBackground(JSONArray... json) {

        String responseMessage = "";
        int counter = 0;

        try {

            CustomMultiPartEntity entity = new CustomMultiPartEntity(new ProgressListener() {
                @Override
                public void transferred(long num) {
                    publishProgress((int) ((num / (float) totalSize) * 100));
                }
            });

            HttpClient httpclient = new DefaultHttpClient();
            HttpPost httppost = new HttpPost(URL);

            //convert JSON array to String (first element contains the whole of data)
            String json_encoded_string = json[0].toString();

            //add json data
            entity.addPart("json", new StringBody(json_encoded_string));

            //get all images plus data and add them to the Multipart Entity

            for (images_cursor.moveToFirst(); !images_cursor.isAfterLast(); images_cursor.moveToNext()) {

                counter++;

                //Get image and convert to byte array
                byte[] image_ba = images_cursor.getBlob(images_cursor.getColumnIndex("image"));
                long image_unit_id = images_cursor.getLong(images_cursor.getColumnIndex("unit_id"));
                String image_caption = images_cursor.getString(images_cursor.getColumnIndex("caption"));

                //add image to multipart
                entity.addPart("image" + counter, new ByteArrayBody(image_ba, "image" + counter + ".jpg"));

                //add unit _id to multipart
                entity.addPart("image_unit_id" + counter, new StringBody(String.valueOf(image_unit_id)));

                //add caption to multipart
                entity.addPart("image_caption" + counter, new StringBody(String.valueOf(image_caption)));

            }

                            totalSize = entity.getContentLength();
            httppost.setEntity(entity);

            HttpResponse response = httpclient.execute(httppost);
            InputStream inputStream = response.getEntity().getContent();
            responseMessage = inputStreamToString(inputStream);

            //log out response from server
            longInfo(responseMessage);

        }
        //show error if connection not working
        catch (SocketTimeoutException e) {
            e.printStackTrace();
            responseMessage = "unreachable";

        }

        catch (ConnectTimeoutException e) {
            e.printStackTrace();
            responseMessage = "unreachable";

        }

        catch (Exception e) {
            e.printStackTrace();
            responseMessage = "unreachable";

        }

        return responseMessage;
    }

    @Override
    protected void onProgressUpdate(Integer... progress) {
        dialog.setProgress((int) (progress[0]));
    }

    @Override
    protected void onPostExecute(String result) {

        //result is the response back from "doInBackground"
        dialog.cancel();

        TextView response_holder = (TextView) findViewById(R.id.response);

        //check if there were errors upoloading
        if (result.equalsIgnoreCase("unreachable")) {
            response_holder.setText("Error while uploading...Please check your internet connection and retry.");
            return;
        }

        if (result.equalsIgnoreCase("error saving project data")) {
            response_holder.setText("There was an error on the server saving the project data, please retry.");
            return;
        }

        if (result.equalsIgnoreCase("error saving sites data")) {
            response_holder.setText("There was an error on the server saving the sites data, please retry.");
            return;
        }

        if (result.equalsIgnoreCase("project saved")) {
            response_holder.setText("Data uploaded successfully!");
            return;
        }

        if (result.equalsIgnoreCase("project already exist")) {
            response_holder.setText("This project already exist!");
            return;
        }

    }

 }

我猜它只是计算JSON字符串的总大小,因为这是第一部分...有什么建议吗?
编辑:可能是重复的问题,但仍然没有解决方案,在这里
编辑:通过在发布实体之前添加totalSize = entity.getContentLength();已解决该问题,代码已更新。
1个回答

3

看一下这段代码:

@Override
protected void onProgressUpdate(Integer... progress) {
    dialog.setProgress((int) (progress[0]));
}

基本上,您只关心第一个进度值。这很好,因为您的代码可能只有一个。但是您可以通过在代码中添加publishProgress语句来发布进度。每次完成某些操作时,可以通过推送百分比来完成此操作,例如:

     int progress_done=20;
     publishProgress(progress_done);
     for (images_cursor.moveToFirst(); !images_cursor.isAfterLast(); images_cursor.moveToNext()) {

            counter++;

            //Get image and convert to byte array
            byte[] image_ba = images_cursor.getBlob(images_cursor.getColumnIndex("image"));
            long image_unit_id = images_cursor.getLong(images_cursor.getColumnIndex("unit_id"));
            String image_caption = images_cursor.getString(images_cursor.getColumnIndex("caption"));

            //add image to multipart
            entity.addPart("image" + counter, new ByteArrayBody(image_ba, "image" + counter + ".jpg"));

            //add unit _id to multipart
            entity.addPart("image_unit_id" + counter, new StringBody(String.valueOf(image_unit_id)));

            //add caption to multipart
            entity.addPart("image_caption" + counter, new StringBody(String.valueOf(image_caption)));

            progress_done+=20;
            publishProgress(progress_done);
        }

AsyncTask的文档并不是最清晰易懂的。http://developer.android.com/reference/android/os/AsyncTask.html#publishProgress(Progress...) - Paranoid Android
是的,我花了很长时间才理解它们的工作原理。基本上,aSyncTask旨在同时处理大量输入。据我所知,这一关键信息在文档中缺失... - PearsonArtPhoto
无论如何,添加您建议的代码都没有改变任何事情。我明天会再试一次:/ - Paranoid Android
我建议您打印出进度值,然后从那里开始。祝你好运! - PearsonArtPhoto
3
问题已解决,原因是缺少了totalSize = entity.getContentLength();这一行代码,导致总大小始终为0。 - Paranoid Android
显示剩余4条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接