编辑:已解决,向下滚动;)

按照教程here,

我在AsyncTask中实现了一个进度条对话框,以显示上传数据的百分比.目前它只是显示从0%到100%,然后它被卡在100%,直到所有数据上传.我正在尝试上传一个Json字符串加上4个图像.

码:

private class UploaderTask extends AsyncTask {

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();在发布实体之前,代码已更新

android 显示多条数据格式,Multipart上传的进度条,包含多个Android文件相关推荐

  1. jquery PHP大文件上传,Jquery和BigFileUpload实现大文件上传及进度条显示

    实现方法:用到了高山来客 的bigfileupload组件,用高山来客的方法,弹出一个模式窗口,然后不停刷新获取进度,始终觉得体验感不好,于是想到用jquery来实现无刷新进度显示,因为提交页面后, ...

  2. ajax实现上传文件的进度,基于Ajax技术实现文件上传带进度条

    1.概述 在实际的Web应该开发或网站开发过程中,经常需要实现文件上传的功能.在文件上传过程中,经常需要用户进行长时间的等待,为了让用户及时了解上传进度,可以在上传文件的同时,显示文件的上传进度条.运 ...

  3. atitit. 文件上传带进度条 atiUP 设计 java c# php

    atitit. 文件上传带进度条atiUP设计java c# php 1. 设计要求 1 2. 原理and架构 1 3. ui 2 4. spring mvc 2 5. springMVC.xml 3 ...

  4. 文件上传 带进度条(多种风格)

    文件上传 带进度条 多种风格 非常漂亮! 友好的提示 以及上传验证! 部分代码: <formid="form1"runat="server">< ...

  5. el-upload使用http-request自定义上传和进度条实战

    介绍 项目中发现使用默认的el-upload上传动作发送上传请求的时候不会带上请求头,于是想通过自定义请求也就是http-request来自定义上传.实践证明这条路是通的,不过有个小问题就是原本上传的 ...

  6. jquery文件上传插件|进度条

    jquery文件上传插件|进度条 一.Uploadify 官网:http://www.uploadify.com/ 下载地址:http://down.51cto.com/data/577863 详细参 ...

  7. Flex4/Flash多文件上传(带进度条)实例分享

    要求 必备知识 本文要求基本了解 Adobe Flex编程知识和JAVA基础知识. 开发环境 MyEclipse10/Flash Builder4.6/Flash Player11及以上 演示地址 演 ...

  8. 文件上传 java 进度条_Java如何实现动态显示文件上传进度条

    本文实例实现文件上传的进度显示,我们先看看都有哪些问题我们要解决. 1 上传数据的处理进度跟踪 2 进度数据在用户页面的显示 就这么2个问题, 第一个问题,主要是组件的选择 必须支持数据处理侦听或通知 ...

  9. 上传文件显示进度条_文件上传带进度条进阶-断点续传

    说明 1. 把文件按大小1M分割成N份 2. 每次上传时,告诉后台大文件的md5.当前第几份(从0开始).总共几份 3. 并行上传,前端同时开启5个请求进行传输增加速度 4. 上传失败或出错后,继续上 ...

  10. MVC 图片上传 带进度条(转)

    MVC 图片上传小试笔记 form.js 这个插件已经是很有名的,结合MVC的html辅助方法异步上传就很简单了.jQuery Form Plugin :http://www.malsup.com/j ...

最新文章

  1. matlab如何求指标的权向量_层次分析模型(AHP)及其MATLAB实现
  2. mysql status改变_mysql 配置详解mysql SHOW STATUS 详解
  3. [转载]让SQL运行得更快
  4. 8086 INC, DEC
  5. 十六进制字符串转整形
  6. php类知识 self $this都只能在当前类中使用
  7. TCP重复ACK与乱序
  8. setw()函数使用
  9. HTTP 多处理模块(MPM)
  10. IE提示当前安全设置不允许下载该文件怎么办?
  11. 形容词,名词记忆(五):ing ,ed 后缀常用词
  12. 基于 STM32对音频数据的 Flash 读取与 DAC 播放
  13. SQL Server 的几个故障
  14. JS 大杂烩(持续更新)
  15. 【MindInsight】在win下安装MindInsight遇到“ No module named apos;fcntlapos;“问题
  16. 数字减影血管造影技术(DSA)
  17. 无线传感网络就业指导
  18. GPS卫星定位车载终端原理全面介绍
  19. Otter 双向同步mysql
  20. 计算机无法正常启动无法修复,win10无法正常启动

热门文章

  1. 这些“大学”,将被除名!
  2. 重磅!国内首个三维重建系列视频课程,涉及SFM、立体匹配、多视图几何、结构光等...
  3. 机器学习中的偏差和方差理解
  4. day042 css 选择器
  5. maven修改本地仓库地址配置文件
  6. linux awk使用详解
  7. run (简单DP)
  8. easymock+junit+spring学习·
  9. [Ogre][地形][原创]基于OgreTerrain的地形实现
  10. 1到20的阶乘倒数之和