1

I am developing an android application that needs to communicate a lot with a remote server. For this task I wrote a class that handles all the network communication.

Do I need to make for every single method as an Asynctask? What about methods that I am dependent on for the rest of the code execution? (thus needs to be done synchronously - like waiting for an answer on registration?)

I am asking this because I already had a small app before to communicate with a remote server and I didn't use any Asynctasks, this one crashes on every method being called though.

Edit -
Meanwhile - before making a class of my own I found a great tutorial related to a google libraray that already handle that the libraray name is Volley the tutorial I looked on is this one

crazyPixel
  • 2,301
  • 5
  • 24
  • 48

2 Answers2

5

Yes, every network call has to asynchronous. You don't need to make every single method in you class async, but i would refactor the code in a way that only one peace of code actually does the calls and this peace has to be async. If you have following code that depends on the response from the server, then use a callback.

In pseudo code that would look something like this:

void makeNetworkCall(command, listener){
    async(){
        result = command.execute();
        listener.onCommandSuccess(result);
    }
}
SimonSays
  • 10,867
  • 7
  • 44
  • 59
  • Do you mean I somehone need to create a generic class extending the asynctask class? isn't there any library that already handle network calls? as for now I have built a class to handle every user command (i.e register, login, delete account and so on) every command return some value according to the JSONObject value it gets from the server, doesn't that means I need to make every method as a sub-class? I'm not really sure I understand your answer even though it seems like a good way to follow... =\ – crazyPixel Feb 15 '14 at 00:14
  • You could create a parent class that all your command classes inherit from. The parent class would have the above method. The child classes assemble the correct command, call the `makeNetworkCall()` method from the parent class and then handle the response that they get back with the listener. – SimonSays Feb 15 '14 at 00:21
  • Thanks bro, much appreciated i'll upload something for the benefit of others once i'll get it done [and also to get some suggestions.. =) ] – crazyPixel Feb 15 '14 at 00:24
0

Do I need to make for every single method as an Asynctask?

Yes. Android requires networking code to be executed asynchronously, so the user interface never gets blocked.

What about methods that I am dependent on for the rest of the code execution?

You can wait for an Asynchtask to finish execution. Refer to this question.

Community
  • 1
  • 1
ntv1000
  • 626
  • 6
  • 16
  • Using `get()` is not a good idea, unless you are calling it on some *other* background thread. Otherwise, you are blocking the main application thread, which is precisely what we are using an `AsyncTask` to avoid. A better solution is to abide by event-driven programming practices, such as in SimonSays' answer. – CommonsWare Feb 14 '14 at 23:55