Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8854015
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T13:47:11+00:00 2026-06-14T13:47:11+00:00

Edit: solved, scroll down ;) Following the tutorial here , I implemented a progress

  • 0

Edit: solved, scroll down 😉

Following the tutorial here,

I implemented a progress bar dialog within an AsyncTask to show the percentage of data uploaded. Currently it is just showing from 0% to 100%, then it gets stuck at 100% until all the data are uploaded. I am trying to upload a Json string plus 4 images.

Code:

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

    }

 }

I am guessing it is just calculating the total size of the json string since it is the first part…any suggestions?

Edit: possible duplicate, still with no solution here

Edit: solved adding totalSize = entity.getContentLength(); before posting the entity, code has been updated

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T13:47:12+00:00Added an answer on June 14, 2026 at 1:47 pm

    Look at this bit of code:

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

    Essentially, you are only caring about the first progress value. That’s fine as there is probably only one from your code. But you could publish the progress by adding publishProgress statements to your code. This could be done by pushing a percentage each time you are done with something, like this:

         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);
            }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

EDIT: Update - scroll down EDIT 2: Update - problem solved Some background information:
EDIT: SOLVED--SOURCE CODE HERE: http://matthewdowney20.blogspot.com/2011/09/source-code-for-roku-remote-hack.html thanks in advance for reading and possibly answering this.
EDIT: solved, look below for my solution. first of all, this is my very
Edit: [Solved] : It seems that the getTableCellRendererComponent of the CustomTableCellRenderer is called every
Edit: Solved. Hi, I'm starting with Qt, I try to connect a slot to
edit: Solved - mod_rewrite was the problem I can't get CI to work as
EDIT: SOLVED Thanks Brooks. Your question led me to keep digging into if the
EDIT: solved it, turns out I should use %c not %s because foodSelect and
[EDIT: Problem solved. Please see my answer below.] In my app I call the
Answer solved in edit below I had this piece of code Dictionary<Merchant, int> remaingCards

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.