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

有帮助吗?

解决方案

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);
    }
}

其他提示

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.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top