I'm doing a REST API
on Android
in which I have more than one AsyncTask
(one for HTTPGet
and one for HTTPPost
) and I didn't have any problem with them but, when I tried to execute my AsyncTask
where I use HTTPDelete
, it doesn't execute doInBackground()
method.
Searching in SO I found this solution: Android SDK AsyncTask doInBackground not running (subclass) but I'm not secure about using .executeOnExecutor
or not because I search in the official documentation: executeOnExecutor and it says that it could be problems.
For example, if these tasks are used to modify any state in common (such as writing a file due to a button click), there are no guarantees on the order of the modifications.
And I'm using my GET
, POST
and DELETE
methods to modify my database
(I know that it's not a file
but I suppose that it could gives to me the same problem).
According to this, I'm not secure about what can I do for execute this .doInBackground()
method because I don't want to have problems in the future.
What should I use?
This is my AsyncTask
:
class DeleteCar extends AsyncTask<Integer, Integer, Void> {
protected void onPreExecute(){
}
protected Void doInBackground(Integer... id) {
try{
String url = "http://IP of my computer/project/cars/" + id[0].intValue();
HttpClient httpClient = new DefaultHttpClient();
HttpDelete method= new HttpDelete(url);
method.setHeader("content-type", "application/json");
HttpResponse response = httpClient.execute(method);
HttpEntity httpEntity = response.getEntity();
}catch(Exception ex){
Log.e("ServicioRest",ex.toString());
}
return null;
}
protected void onProgressUpdate(){
}
protected void onPostExecute(){
}
}
And I execute it like this:
new DeleteCar().execute(idCar);
where idCar
it's Integer idCar = new Integer(id);
and id
it's an int
.
Note: It enters at onPreExecute()
method, but not in the doInBackground() method
.