I’m going to have around 10 or even more http post requests in my android app most of them are very short-time.
As I understand, I cannot perform any network operations on my UI thread.
The solution for this would be using the AsyncTask or using Thread.
If I’ll be using the AsyncTask I will have to create to a customized class for that request.
If I’ll be using Thread I will not have to implement a new class for the network operation.
So, my question is, when should I use the Thread and when should I use the AsyncTask option?
Is it the right approach to create a thread for the short-time requests, and create a class that extends the AsyncTask on the longer-lived network operations?
Even if you use
Thread.start(), you still need to create a subclass ofRunnable(or a subclass ofThread) to do the work in. You are creating new classes either way – maybe the only difference is if you are creating anonymous classes versus a top-level named class.You shouldn’t base your decision on whether or not you need to make a new class, since the cost is tiny – you create a new file, it has a different structure, etc. It’s a simple one-time cost.
It seems to me like the decision should be based on whether or not you want all the extra logic and functionality that AsyncTask offers.