-1

In my app I've got a class that i use to get data from my server. It works fine in every Activity that i made, but this time i want to use it in Service subclass, which is (services) new to me in 100%.

I found an issue in this line:

InputStream inputStream = urlConnection.getInputStream();

This is a place when my web class fires Exception:

android.os.NetworkOnMainThreadException

Is there any major difference between using it in activities and services?

cyborg86pl
  • 2,597
  • 2
  • 26
  • 43

2 Answers2

1

The error android.os.NetworkOnMainThreadException means you are running a network oriented task on the main thread "UI Thread". Android now forbids this as it renders the UI unresponsive during the network call.

To solve this you need to make the network call in another thread. AsyncTask is perfectly suited to this.

jim
  • 8,670
  • 15
  • 78
  • 149
-1

Ok i figured it out, This was the problem. I used AsyncTask in my Service and it runs well now.

Community
  • 1
  • 1
cyborg86pl
  • 2,597
  • 2
  • 26
  • 43