我根本无法中止图像上传。我已经用谷歌搜索了很多次,以寻找任何方法来中止 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]));
}
}