Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android best practice AsyncTask

I am new to Android Development. Sorry for a dumb question.

I need to check several http resources (resource1, resource2... etc) and check if they are up. If resource 1 is unavailable, then the app needs to check if internet is up (probably ping google?) and then if the connection is actually working it needs to check all the other resources and place the information about what is down to the notification drawer.

It's pretty straightforward to do it with AsyncTask and HttpURLConnection for one resource but i don't quite understand how to follow execution logic with async calls (resource1 -> google -> resource2 -> gather info in one place -> display notification). What is the best practice for it?

like image 470
Fyodor Dostoyevsky Avatar asked Feb 08 '14 21:02

Fyodor Dostoyevsky


Video Answer


1 Answers

@FD_'s answer is valid but I think there is a better way that avoids the horrors of the Async Task. For the record I upvoted FD_'s answer as it is valid I am just pointing out another way.

So I would make a result object. This can be used to work out what happened when you tried to communicate with the various services. You could have something like

public class ResponseResult {

    public String displayName; // so we know what this was all about e.g. "google request"
    public boolean success;
    public String message;
    public int httpStatusCode; //useful to help workout what went wrong e.g. 500, 302 etc.

}

You could then use something like the Android Volley Library that is better than an AsyncTask as it uses a RequestQueue in another thread, which survives the Activity lifecycle better. It also might make your code a little easier to read/manage.

You can read a little about Volley Vs AsyncTask here - Volley and AsyncTask

Once you have issues all your requests and put the results in an Array or List, you could then iterate over them to print the result.

You can get Volley from here https://android.googlesource.com/platform/frameworks/volley

Additional Info

You might also find this inforgraphic from Robospice to be useful as it helps to explain the drawbacks of an AsyncTask.

https://raw.github.com/octo-online/robospice/master/gfx/RoboSpice-InfoGraphics.png

Another implementation

You may find this implementation is not suitable but it makes some sense and would produce even less code.

You could write some server side code to do the checks for you and return an XML/JSON array of result objects. This has the advantage of a single request to a hardwired server with a more reliable connection, that possibly could make the requests in a shorter space of time.

Your Android device would only issue a single request to the server and then process the result array per my other method above.

The major drawback is that this would introduce another set of code and additional hardware.

like image 73
Graham Smith Avatar answered Oct 04 '22 04:10

Graham Smith