2012-10-31 20 views
0

我的代码中使用多这样的上传,在这段代码中我有添加进度条进度条不更新,其中上传多

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

但是当我运行这段代码,进度条没有显示进度条不更新过程。我只看到%0和%100。如何解决这个??你能帮忙吗?

谢谢

+0

看看我的【答案】(http://stackoverflow.com/a/13087101/996493)。 – Lucifer

+0

感谢,但这些代码更新进度因延误,而不是在我成功上传到服务器,可你给我ither建议,谢谢:) –

+0

@yaLya你解决这个问题?我有同样的问题 – Mirko

回答

0

您上传的数据有多大(以字节为单位)?它可能很小,以至于进度监听器只会被调用一次:完成时。

+0

,你觉得我的代码是正确的? –

0

totalSize可能是0,尝试登录了这一点在你的publishProgress

也尝试登录该totalSize = reqEntity.getContentLength();

+0

你解决了吗? –

+0

没有@Mirko,我检查,我总计TOTALSIZE具有价值, –

+0

@yaLya,yes..mine现在工作,看看http://goo.gl/7v94I – Mirko