尝试从我的android服务调用网络服务时出现android.os.NetworkOnMainThreadException异常?

时间:2020-07-07 12:28:49

标签: android mvvm android-asynctask android-jetpack android-async-http

我有一个需要从Android服务调用Web服务的场景。 目前,我是通过“

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

但是我得到了一个异常,如android.os.NetworkOnMainThreadException。当我遵循MVVM Architecture时,是否可以通过AsyncTask和其他任何东西来调用它?

1 个答案:

答案 0 :(得分:0)

服务在UI线程上运行。如果您需要服务扩展IntentService或新的JobIntentService而不是ServiceIntentServiceonHandleIntent)和JobIntentServiceonHandleWork)都将在UI / Main线程的线程!=上执行

相关问题