What is the best way to handle HttpClient on Android?

I can find many tutorials that show how HttpClient should be designed and used in a simple case. But I can not find decent documentation for more complex cases.

In my application, I have a bunch of Acts, and each of them should be able to communicate with the remote WebService with POST messages using HTTPS. I also have to log in to this service, so I need to manage the cookie to log in.

I am currently creating a separate HttpClient for each individual Activity in .onStart () and freeing it in .onStop (). I have a work queue that contains objects that describe the details of a task that should contact the remote service. I perform these tasks using a single working AsyncTask. This solution seems to work, but I'm just not sure if it is the most optimal one.

I thought of two other architectures:

  • Create a help desk that handles HttpClient. That way, I could use the same instance across several activities, and I think it would be better. But I'm not sure when to stop this service and release HttpClient.
  • Create HttpClient only when necessary. For example, when a user presses a button that initiates a remote call, I create a client, configure cookies and a POST message, execute it, and when this is done, I immediately release it. I think this approach is pretty bad due to the overhead associated with creating such a client (especially if I use HTTPS).

So, anyone who has more information about the operation of HttpClient and how it should be considered in Android, could you comment on these statements / share some useful tips?

thanks

+4
source share
2 answers

solution 1 is good. You can stop your service when your turn is empty. It combines with your solution 2 in that the service starts only when necessary. When the user clicks the button, you start your service and, for example, become attached to it and pass your request to it.

+2
source

AsyncTask. Search for the GrabURl method on Google.

-2
source

All Articles