进度条不更新,其中多部分上传 [英] progress bar not update where upload multipart
本文介绍了进度条不更新,其中多部分上传的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有code使用多这样的上传,在此code我有加进度条
I have code to upload using multipart like this , in this code I have add progress bar
protected String doInBackground(String... args) {
namapro = inputtitle.getText().toString();
hargapro = inputprice.getText().toString();
despro= inputdes.getText().toString();
orderpro = inputorder.getText().toString();
path = getIntent().getStringExtra("pathimage");
product.setTitle(namapro);
product.setPrice(hargapro);
product.setDesc(despro);
product.setHow(orderpro);
product.setImage(path);
try{
CustomMultipartEntity multipartContent = new CustomMultipartEntity(new ProgressListener()
{
@Override
public void transferred(long num)
{
int upload = ((int)((num/(float)totalSize)*100));
String progress = String.valueOf(upload);
publishProgress(progress);
}
});
HttpEntity resEntity;
HttpContext httpContext = new BasicHttpContext();
HttpClient httpClient = new DefaultHttpClient();
HttpPost post = new HttpPost(Constants.url_create_product);
MultipartEntity reqEntity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
File file= new File(path);
FileBody bin = new FileBody(file);
reqEntity.addPart("phone", new StringBody(mPhoneNumber));
reqEntity.addPart("prod_title", new StringBody(namapro));
reqEntity.addPart("prod_price", new StringBody(hargapro));
reqEntity.addPart("prod_desc", new StringBody(despro));
reqEntity.addPart("prod_order", new StringBody(orderpro));
reqEntity.addPart("prod_image", bin);
totalSize = reqEntity.getContentLength();
post.setEntity(reqEntity);
HttpResponse response = httpClient.execute(post);
resEntity = response.getEntity();
String response_str = EntityUtils.toString(resEntity);
Gson gson = new Gson();
gson.toJson(response_str);
if (resEntity != null) {
//Log.i("RESPONSE",response_str);
runOnUiThread(new Runnable(){
public void run() {
try {
if(daftar.equalsIgnoreCase("2")){// sudah connect fb twitter
Intent i = new Intent(productdetail.this, home.class);
Toast.makeText(getApplicationContext(), "Upload success", Toast.LENGTH_LONG).show();
startActivity(i);
finish();
}
else if(daftar.equalsIgnoreCase("1")){ // belum connect fb dan twitter
dialog();
}
}catch (Exception e) {
e.printStackTrace();
}
}
});
}
}catch (Exception ex){
Log.e("Debug", "error: " + ex.getMessage(), ex);
dialog_gagal();
}
return null;
}
protected void onPostExecute(String file_url) {
// dismiss the dialog once done
pDialog.dismiss();
}
protected void onProgressUpdate(Integer...progress){
pDialog.setProgress((int) (progress[0]));
//pDialog.setProgress(pDialog.getMax()-bytesAvailable);
}
但是,当我运行这个code,进度条没有显示的进度不更新的过程。我刚才看到%0和100%。如何解决这个问题? u能帮助?
But when I run this code , the progress bar didnt show the progressBar not updating on process. I just see %0 and %100. How to solve this?? can u help ?
感谢
推荐答案
您总计TOTALSIZE
可能是0,尝试登录了这一点在你的publishProgress
Your totalSize
is probably 0, try to log that out in your publishProgress
也尝试登录该总计TOTALSIZE = reqEntity.getContentLength();
这篇关于进度条不更新,其中多部分上传的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文