如何中止或取消图片上传HttpPost?

时间:2013-04-23 16:40:02

标签: java android http-post android-image

我根本无法中止图片上传。我用Google搜索了很多次搜索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 个答案:

答案 0 :(得分:1)

问题是如果你在onCancelled()完成后调用你的任务doInBackground()。这意味着如果您开始在doInBackground()上传图像,则在上传完成之前,任务不会被中断。所有调用cancel()的方法都是通过调用`onCancelled()来阻止onPostExecute()运行。

如果您在任务上致电cancel(true),然后在isCancelled()方法中检查任务。doInBackground(),您应该可以中断doInBackground()并停止上传。