Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Good design patterns for coding many HTTP requests in Android

In my app, i have lots of GET,POST, PUT requests. Right now, i have a singleton class that holds my downloaded data and has many inner classes that extend AsyncTask. In my singleton class, i have also a few interfaces like this:

/**
* Handlers for notifying listeners when data is downloaded
* 
*/
public interface OnQuestionsLoadedListener {

    public void onDataLoadComplete();

    public void onDataLoadingError();       
}

Is there something wrong with this pattern (many inner classes that extend AsyncTask)? Could it be done more efficiently with maybe just 1 inner class for every HTTP call (1 for GET, 1 for POST, ...)? If so, how to decide what to do after e.g. GET request?

like image 925
DixieFlatline Avatar asked Mar 22 '13 12:03

DixieFlatline


People also ask

Which design pattern suggest multiple classes through which request is passed and multiple?

6. Which design pattern suggests multiple classes through which request is passed and multiple but only relevant classes carry out operations on the request? Explanation: Chain of responsibility pattern creates a chain of receiver objects for a particular request.

What are some common design patterns you see in Android framework?

So in this blog, we learned about some of the Design Patterns that are used in Android to write a cleaner and understandable code. We learn about Builder pattern, Singleton pattern, Dependency Injection pattern, Adapter pattern, Facade pattern, Observer pattern, Command pattern, MVC, MVP, and MVVM pattern.

What is the best approach in design patterns in coding?

Expert-Verified Answer. One of the most popular design patterns used by software developers is a factory method. It is a creational pattern that helps create an object without the user getting exposed to creational logic. The only problem with a factory method is it relies on the concrete component.


1 Answers

As a whole, you should get away from AsyncTasks while preforming network requests.

Your AsyncTasks are linked to your Activity. That means, if your Activity stops, your AsyncTask stops. This isn't the biggest problem when fetching data to show in that Activity, since you won't care that the fetching has stopped. But when you want to send some saved data to the server, and your user pressed 'back' or something like that before everything is sent, the data could be lost and not send.

What you want to have instead, is a Service which will keep running regardless of what happens to your Activities.

I'd advise you to take a look into RoboSpice. Even if you decide not to use it, reading what it does and why it does will give you a good insight on the pretty long list of reasons not to use AsyncTasks for network requests and why better to use Services. If you use this, the rest of your question about efficiently network requesting is obsolete too, since they'll handle it for you the best way possible.

like image 51
Stefan de Bruijn Avatar answered Sep 29 '22 07:09

Stefan de Bruijn