按照教程here,
我在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字符串的总大小,因为它是第一部分…任何建议?
编辑:可能重复,仍然没有解决方案here
编辑:已解决添加totalSize = entity.getContentLength();在发布实体之前,代码已更新
解决方法 看看这段代码:@OverrIDeprotected 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,"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); }总结
以上是内存溢出为你收集整理的Multipart上传的进度条,包含多个Android文件全部内容,希望文章能够帮你解决Multipart上传的进度条,包含多个Android文件所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)