I make a aplication on android, with contains a service to request server with interval\_time. The service run clearly when my app is open. And when I close/stop my app, i got a error in connection.connect(); I want myService can run clearly when my app open or close.

My Code:

```
public class RequestService extends Service {

   String interval_config;

   private Handler mHandlerConfig = new Handler();

   private Timer mTimerConfig = null;
   public static long CONFIG_INTERVAL=10;

   @Nullable
   @Override
   public IBinder onBind(Intent intent) {
      return null;
   }

 @Override
 public void onCreate() {
        if(mTimerConfig != null){
            mTimerConfig.cancel();
        } else {
            mTimerConfig = new Timer();
        }
        mTimerConfig.scheduleAtFixedRate(new ConfigTimerTask(), 0, CONFIG_INTERVAL);
   }

 class ConfigTimerTask extends TimerTask {
    @Override
    public void run() {
        // run on another thread
        mHandlerConfig.post(new Runnable() {

            @Override
            public void run() {
                URL url = new URL(/*URL address*/);
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            connection.setReadTimeout(15000);
            connection.setConnectTimeout(15000);
            connection.setRequestMethod("POST");
            connection.setDoInput(true);
            connection.setDoOutput(true);
            connection.connect();  //error here
            }

        });
    }

  }

}

```

How i run my service:

```
startService(new Intent(this, RequestService.class));

```

Error:

```
2517-2517/com.sia.siav2 E/AndroidRuntime﹕ FATAL EXCEPTION: main
android.os.NetworkOnMainThreadException
        at      android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1117)
        at java.net.InetAddress.lookupHostByName(InetAddress.java:385)
        at java.net.InetAddress.getAllByNameImpl(InetAddress.java:236)
        at java.net.InetAddress.getAllByName(InetAddress.java:214)
        at libcore.net.http.HttpConnection.<init>(HttpConnection.java:70)
        at libcore.net.http.HttpConnection.<init>(HttpConnection.java:50)
        at libcore.net.http.HttpConnection$Address.connect(HttpConnection.java:340)
        at libcore.net.http.HttpConnectionPool.get(HttpConnectionPool.java:87)
        at libcore.net.http.HttpConnection.connect(HttpConnection.java:128)
        at libcore.net.http.HttpEngine.openSocketConnection(HttpEngine.java:316)
        at libcore.net.http.HttpEngine.connect(HttpEngine.java:311)
        at libcore.net.http.HttpEngine.sendSocketRequest(HttpEngine.java:290)
        at libcore.net.http.HttpEngine.sendRequest(HttpEngine.java:240)
        at libcore.net.http.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:81)
        at com.sia.siav2.service.RequestService.jsonFunction(RequestService.java:277)
        at com.sia.siav2.service.RequestService.memintaIntervalNotif(RequestService.java:141)
        at com.sia.siav2.service.RequestService$ConfigTimerTask$1.run(RequestService.java:106)
        at android.os.Handler.handleCallback(Handler.java:725)
        at android.os.Handler.dispatchMessage(Handler.java:92)
        at android.os.Looper.loop(Looper.java:137)
        at android.app.ActivityThread.main(ActivityThread.java:5041)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:511)
        at   com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
        at dalvik.system.NativeStart.main(Native Method)

```