I am using unbounded service for sending the request to server but it is showing Network Main Thread Exception. Now i am applying the same logic with Async Task its working fine. Can we get response from server by using unbounded service ?
Asked
Active
Viewed 53 times
-1
-
Are you perform network operation into onStatCommand? – an_droid_dev Jun 12 '17 at 10:15
-
You can do it but it is not a good practice see this https://stackoverflow.com/a/9289190/6869086 – ice spirit Jun 12 '17 at 10:52
-
@an_droid_dev i am performing network operation in onStartCommand() i want to hit some url and get the JSON as a response from onStartCommand() without using Asynctask inside onStartCommand() or any thread. – Gyan Swaroop Awasthi Jun 15 '17 at 10:58
1 Answers
1
This happens because you are doing a network operation on the main thread, and this is not allowed on Android 3.0 and above. Even though it is in a service, services are run on the UI thread unless you specifically launch them in another thread or create a thread inside it.
You can fix this by running the task in a service off the main UI thread, by using a Thread or an AsyncTask.
Try creating a new thread in onStartCommand()

Arun Shankar
- 2,295
- 16
- 20