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

DrkStr
  • 1,752
  • 5
  • 38
  • 90
  • use Google Volley Library for requesting multiple request at a time .http://captechconsulting.com/blog/clinton-teegarden/android-volley-library-tutorial – Ruban Sep 25 '14 at 05:52

3 Answers3

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

Community
  • 1
  • 1
nobalG
  • 4,544
  • 3
  • 34
  • 72
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

Sheetal Suryan
  • 495
  • 2
  • 7
  • 19
  • **ANR** for the first approach ?? I don't think so, (If handled with a little care) – nobalG Sep 25 '14 at 05:49
  • yeah, its ture, but your problem is have to make 3 http call , here link for anr http://stackoverflow.com/questions/13053611/application-not-responding-anr-executing-service-android – Sheetal Suryan Sep 25 '14 at 05:52
  • @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. – DrkStr Sep 25 '14 at 05:55
  • 1
    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\) – nobalG Sep 25 '14 at 05:55
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.

mmlooloo
  • 18,937
  • 5
  • 45
  • 64
  • 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. – nobalG Sep 26 '14 at 04:30
  • @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. – mmlooloo Sep 26 '14 at 06:14
  • Creating threads is not an overhead,but handling exceptions is.. :) – nobalG Sep 26 '14 at 06:17
  • `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? – mmlooloo Sep 26 '14 at 06:23
  • 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. – nobalG Sep 26 '14 at 06:30