2013-04-23 29 views
-1

我无法中止图像上传。我已经搜索了很多次搜索任何方式来中止HttpPost,并且都失败了。如果我取消仍在上传的过程映像,请告诉它已完成。请一些帮助,下面是我的代码是什么乱七八糟的我:如何中止或取消图片上传HttpPost?

private class shareIt extends AsyncTask<HttpResponse, Integer, HttpResponse> { 
    long totalSize; 
    String serverResponse = null; 
    ProgressDialog pd; 
    String filepath, ar_name, en_name, serverurl = null; 
    JSONObject jsonr = null; 

    HttpClient httpClient; 
    HttpContext httpContext; 
    HttpPost httpPost; 

    public shareIt(String filepath, int share_code) { 
     this.filepath = filepath; 
     this.serverurl = Api.upload_url(share_code); 
    } 

    public shareIt(String filepath, int share_code, String ar_name, String en_name) { 
     this.ar_name = ar_name; 
     this.en_name = en_name; 
     this.filepath = filepath; 
     this.serverurl = Api.upload_url(share_code) + "&ar_name="+ar_name+"&en_name="+en_name; 
    } 

    @Override 
    protected void onPreExecute() { 
     pd = new ProgressDialog(File_Share.this); 
     pd.setMessage("hora"); 
     pd.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); 
     pd.setCancelable(false); 
     pd.setIndeterminate(false); 
     pd.setIcon(android.R.drawable.ic_menu_upload); 

     //cancel button 
     pd.setButton(DialogInterface.BUTTON_NEGATIVE, "cancel", new DialogInterface.OnClickListener() { 
      @Override 
      public void onClick(DialogInterface dialog, int which) { 
       pd.dismiss(); 
       cancel(true); 
      } 
     }); 

     pd.show(); 
    } 

    @Override 
    protected HttpResponse doInBackground(HttpResponse... arg0) { 
     httpClient = new DefaultHttpClient(); 
     httpContext = new BasicHttpContext(); 
     httpPost = new HttpPost(serverurl); 

     try { 
      CustomMultiPartEntity multipartContent = new CustomMultiPartEntity(new ProgressListener() { 
       @Override 
       public void transferred(long num) { 
        publishProgress((int) ((num/(float) totalSize) * 100)); 
       } 
      }); 

      multipartContent.addPart("file", new FileBody(new File(filepath))); 
      totalSize = multipartContent.getContentLength(); 

      httpPost.setEntity(multipartContent); 
      HttpResponse response = httpClient.execute(httpPost, httpContext); 

      serverResponse = EntityUtils.toString(response.getEntity()); 

      return null; 

     } catch (Exception e) { 
      Log.e("uploader", "Uploading Error : "+ e.getMessage()); 
     } 

     return null; 
    } 
    @Override 
    protected void onCancelled() { 
     httpPost.abort(); 
     httpClient.getConnectionManager().shutdown();  
    } 

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

回答

1

的问题是,如果你绝对CANCLE你的任务onCancelled()之后doInBackground()完成调用。这意味着如果你开始上传你的图片doInBackground(),任务将不会被中断,直到上传完成。所有调用cancel()都是通过调用`onCancelled()'代替防止运行。

如果您对任务调用cancel(true),然后检查任务。 isCancelled()从您的doInBackground()方法您应该能够中断doInBackground()并停止上传。

+1

我已经做好了,但仍然图像不会停止上传,同样的问题! – 2013-04-23 18:11:28