android.os.NetworkOnMainThreadException exception while trying to call a webservice from my android Services?

87 Views Asked by At

I have a scenario where I need to call the Webservices from the Android Services. Currently I am starting the Services from the Activity by

Intent mServiceIntent = new Intent(this, AServices.class);
            if (!isMyServiceRunning(AServices.class)) {
                startService(mServiceIntent);
            }

But I am getting an exception as android.os.NetworkOnMainThreadException. As I am following MVVM Architecture so is there any way or alternative that I can call it from AsyncTask and any other stuffs?

1

There are 1 best solutions below

0
Blackbelt On

Service runs on the UI Thread. If you need a Service extend IntentService or the newly JobIntentService and not Service. Both IntentService (onHandleIntent) and JobIntentService (onHandleWork) will be executed on thread != from the UI/Main thread