0

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.

Community
  • 1
  • 1
Francisco Romero
  • 12,787
  • 22
  • 92
  • 167
  • Is idCar on your execute method an integer object? – Omar Faroque Anik Aug 18 '15 at 15:12
  • @MdOmarFaroqueAnik Yes, it is. It's an `int idCar = 5;` – Francisco Romero Aug 18 '15 at 15:14
  • int is primitive. Integer is an object. please try to use new Integer(5) like this way if it works. – Omar Faroque Anik Aug 18 '15 at 15:15
  • @MdOmarFaroqueAnik Oh, sorry. I though you were refearing to `id` inside the `doInBackground()` method. `idCar` it's `Integer idCar = new Integer(id)` where `id` it's an `int`. – Francisco Romero Aug 18 '15 at 16:29
  • Use retrofit my friend. It is really faster. I gave you the link below. Thanks – Omar Faroque Anik Aug 18 '15 at 16:32
  • @MdOmarFaroqueAnik Yes, I saw it and I'm very grateful with you but I need to use in this project `HttpDelete` mandatorily. Any other help with this method will be really appreciated. – Francisco Romero Aug 18 '15 at 16:52
  • u can not sure your url.. so i can not make it run.. otherwise i can write the code for you.. if you can share your url: omarf.android@gmail.com. Sorry I should ask you for that but i am trying to help you. – Omar Faroque Anik Aug 18 '15 at 17:19
  • You are returning null from doInBackgroundMethod which is coming to your onPostExecute method. I don't know what do you want to achieve. – Omar Faroque Anik Aug 18 '15 at 17:24
  • Check my update answer. – Omar Faroque Anik Aug 18 '15 at 17:29
  • @MdOmarFaroqueAnik I'm returning null to onPostExecute because I don't need to do nothing there (all I need it's doing at doInBackground() method). And I can't put the url because I'm in a local server, it's not a public server so anyway you could access to it. I'm using Slim framework and I have a delete method created there (please let me know if you need it and I can provide it to you). I saw your answer and I put a log like you in onPreExecute and in doInBackground methods, it's why I know that it enters into the onPreExecute method but not on doInBackground(). I expect it clarify a bit. – Francisco Romero Aug 18 '15 at 20:22

1 Answers1

-1

Here is a dummy code for you:

public class DeleteCar extends AsyncTask<Integer,Integer,Void> {
@Override
protected Void doInBackground(Integer... params) {

    //logging to show that it is working
    Log.d("result:", String.valueOf(params[0].intValue()));


    return null;
}

}

I have created different class for my AsyncTask. and then i called it from my activity like this:

new DeleteCar().execute(new Integer(10));

It is perfectly working/executing the doInBackground method.

My suggestion will be to use retrofit. Here is couple link to learn retrofit: 1. http://square.github.io/retrofit/ 2. Here is one comparison : https://androidtutorialmagic.wordpress.com/android-asynctask-and-its-dark-side/comparison-among-asynctaskvolley-and-retrofit/ 3. here is a basic tutorial: https://androidtutorialmagic.wordpress.com/android-asynctask-and-its-dark-side/retrofit-networking-in-android-tutorial-replacement-of-asynctask-and-volley/

I have been using Retrofit. I am getting really faster response. If you need more help to use retrofit, please let me know. Thanks

Omar Faroque Anik
  • 2,531
  • 1
  • 29
  • 42