Progress bar for multi-boot with multiple Android files

Edit: enabled, scroll down;)

Following the tutorial here ,

I implemented a progress bar dialog in AsyncTask to show the percentage of downloaded data. Currently it only shows from 0% to 100%, then it gets stuck at 100% until all the data is downloaded. I am trying to load 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 assume it just calculates the total json string size since this is the first part ... any suggestions?

Edit: it is possible to duplicate, still without a solution here

Edit: decided to add totalSize = entity.getContentLength(); before publishing the object, the code is updated

+4
source share
1 answer

Look at this bit of code:

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

Essentially, you only care about the first meaning of progress. This is great because there is probably only one of your code. But you can publish progress by adding publishProgress to your code. This can be done by clicking the percentage each time you finish something like:

  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); } 
+3
source

Source: https://habr.com/ru/post/1447040/


All Articles