4

I have an app underdevelopment and I need to do 3 HTTP POSTs in sequence. What is the best way to implement this ? Should I

  1. Make each HTTP Post in it own Async Class and daisy chain the Async classes(i.e. call the second async from the onPostExecute of the first Async)

  2. Put all the HTTP POSTs in the doInBcakGround of a single Async.

I know how to do a HTTP POST request and I am using the OKHTTP lib. I just would like to know that the best practice it for multiple POSTs in sequence.

Cheers

1

3 Answers 3

3

Your first approach will be better and quite modular as you can keep the track of anything in your application.In the three different AsyncTask you can have a check in postExceute() that which AsyncTask is done with its work (more easily and precisely) AND

>>>>>In case if the application gets Crashed

then which of the httpPost failed. However , the second one will make your code messy and you will be unable to track on getting Exception that which httpPost request failed(in a straight forward way though).


So Launching your second AsyncTask from onPostExecute of your first task will be better approach.

See here too : Calling an AsyncTask from another AsyncTask

Sign up to request clarification or add additional context in comments.

1 Comment

I am going with this for the now. I may come back to Sheetal Suryan's answer later.
0

Both 1 and 2 approaches make app ANR, so better to for other approach.

You can use ExecutorService that executes each submitted task using one of possibly several pooled threads, normally configured using Executors factory methods.

Thread pools address two different problems: they usually provide improved performance when executing large numbers of asynchronous tasks, due to reduced per-task invocation overhead, and they provide a means of bounding and managing the resources, including threads, consumed when executing a collection of tasks. Each ThreadPoolExecutor also maintains some basic statistics, such as the number of completed tasks.

here is more deatias http://developer.android.com/reference/java/util/concurrent/ThreadPoolExecutor.html

4 Comments

ANR for the first approach ?? I don't think so, (If handled with a little care)
yeah, its ture, but your problem is have to make 3 http call , here link for anr stackoverflow.com/questions/13053611/…
@SheetalSuryan You only get an ANR if you run it on the main UI thread, I am doing the HTTP requests on the background thread. Also, I did some quick reading on ExecutorService. From what I can tell the are only used if you want to execute simultaneously, is this correct.
Read the link you posted carefully.....here in OP question no UI thread thing is involved(the link you posted is dealing with the ANR originating from the long Running btask on the UI thread)
0

Put all the HTTP POSTs in the doInBcakGround of a single Async.

because all your posts handle in one module.

dose not have overhead of creating asyntask and GC may not call as many as your first.

you do not need to check internet connection 3 times you have to check it one time

all exception handles one time but in approach 1 you have to copy and paste all exception handler that may occurs. and always recommended not to repeat yourself. if you can do something to not copy and paste codes, do it.

At the end I prefer approach 2.

5 Comments

Don't you think calling three httpPosts together in AsyncTask will be an overhead(I am not saying its not possible though).But is not the efficient way either EVEN if the context of Exception Handling is considered.
@Butterflow you are welcome with your answer but it dose not have overhead of creating 3 threads. it dose not have overhead of creating 3 sockets.
Creating threads is not an overhead,but handling exceptions is.. :)
Creating threads is not an overhead why? OS must manage your threads differently, Os must provide mechanism to treat it in a different way, each threads has a different stack and different programing counter and so on. dose not creating 3 sockets have an overhead?
You are correct though,but on the practical note, most of the time you will want to get rid of the exception first by knowing that which httpPost generated it and handling it in your own way...3 requests in a single AsyncTask is possible, but again (from my personal experience) not the best way.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.