Shashikant
Shashikant

Reputation: 21

Is services is the best way to make http request in android

Is services is the best way to make http request to php server in android? I am currently using the AsycTask but it sometimes unfortunately stops the application so i am planning to use services.

Upvotes: 0

Views: 799

Answers (2)

SilentKnight
SilentKnight

Reputation: 14021

Http request only can be done in a non-UI thread. Because it may cause ANR(Application Not Responding) and is forbidden in the main thread in Android. And for Service, it's run on main thread by default except you set android:process="thread-name" when you deploy it in AndroidManifest.xml. So, If you want to handle with http request on your own codes, you must use AsyncTask, Handler or HandlerThread. But I recommend you to use Google Volley, android-async-http or okHttp. Because they are encapsulated well and easy to use and take care everything for you.

Upvotes: 0

Varundroid
Varundroid

Reputation: 9234

You have to have good reasons for using any of them. If you want to perform something in background while app is at sleep then surely service is the way to go. AsyncTask lifecycle is based on Activity lifecycle. If Activity dies and you have some pending code inside your AsyncTask then it can cause a crash so cancelling AsyncTask before exiting the app is a good practice.

But my recommendation and the best way is here to use Google Volley Library for making http request. It takes care of everything for you. You can easily cancel the request in case of exiting the app.

Upvotes: 1

Related Questions