Implemnting Asynctask in my Aplication, But my AsyncTask still running when cancelled and not stop. so how to solve it ? this my code
when I running it
myTask = new ImageUploadTask();
myTask.execute();
this Asyntask (partly based on this answer by kuester2000)
class ImageUploadTask extends AsyncTask <Void, Void, String>{
String err=null;
@Override
protected String doInBackground(Void... unsued) {
for(int i = 0; i <= 30000; i++) {
if(myTask.isCancelled()) break;
}
try {
ByteArrayOutputStream bos_1 = new ByteArrayOutputStream();
bitmap_photo_1.compress(CompressFormat.JPEG, 100, bos_1);
byte[] data_1 = bos_1.toByteArray();
HttpParams httpParameters = new BasicHttpParams();
// Set the timeout in milliseconds until a connection is established.
int timeoutConnection = 30*1000;
HttpConnectionParams.setConnectionTimeout(httpParameters, timeoutConnection);
// Set the default socket timeout (SO_TIMEOUT)
// in milliseconds which is the timeout for waiting for data.
int timeoutSocket = 30*1000;
HttpConnectionParams.setSoTimeout(httpParameters, timeoutSocket);
HttpClient httpClient = new DefaultHttpClient(httpParameters);
HttpPost postRequest = new HttpPost(Variabel.URL_CONFIRM_ACTION);
ByteArrayBody bab_1 = new ByteArrayBody(data_1,var_photo_1);
MultipartEntity reqEntity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
reqEntity.addPart(Variabel.KEY_USER_ID, new StringBody(Variabel.user_id.toString().trim()));
reqEntity.addPart(Variabel.KEY_PHOTO_1, bab_1);
reqEntity.addPart(Variabel.KEY_PHOTO_1_TIME, new StringBody(var_photo_1_time.toString().trim()));
reqEntity.addPart(Variabel.KEY_PHOTO_1_LOCATION_LONGITUDE, new StringBody(var_photo_1_location_longitude.toString().trim()));
reqEntity.addPart(Variabel.KEY_PHOTO_1_LOCATION_LATITUDE, new StringBody(var_photo_1_location_latitude.toString().trim()));
postRequest.setEntity(reqEntity);
HttpResponse response = httpClient.execute(postRequest);
BufferedReader reader = new BufferedReader(new InputStreamReader(response.getEntity().getContent(),"UTF-8"));
String sResponse;
StringBuilder s = new StringBuilder();
while ((sResponse = reader.readLine()) != null) {
s = s.append(sResponse);
}
return s.toString().trim();
} catch (Exception e) {
err="error"+e.getMessage();
Log.e(e.getClass().getName(), e.getMessage());
return e.getMessage();
}
}
Canceling code
myTask.cancel(true);
so how to stop full ? sorry for my english