title: Java android simple http get method
body: <p>We are building an Android app to control a little light conneted to a webserver.</p>
  
  <p>The purpose is when we click the button there will be send a simple httpGETrequest to: <code>http://192.168.2.179/?led1</code></p>
  
  <p>We have tried a lot of different ways, and none of them work.</p>
  
  <p>Do you know a simple way to implement this for our project?</p>
  
  <pre><code>public void lihtbulp(View v) throws IOException, URISyntaxException {
  
      InputStream is = null;
      // Only display the first 500 characters of the retrieved
      // web page content.
      int len = 500;
  
      try {
          URL url = new URL("http://192.168.2.179/?led1");
          HttpURLConnection conn = (HttpURLConnection) url.openConnection();
          conn.setReadTimeout(10000 /* milliseconds */);
          conn.setConnectTimeout(15000 /* milliseconds */);
          conn.setRequestMethod("GET");
          conn.setDoInput(true);
          // Starts the query
          conn.connect();
          int response = conn.getResponseCode();
          Log.d(DEBUG_TAG, "The response is: " + response);
          is = conn.getInputStream();
  
          // Convert the InputStream into a string
  
  
          // Makes sure that the InputStream is closed after the app is
          // finished using it.
      } finally {
          if (is != null) {
              is.close();
          }
      }
  }
  </code></pre>
  
  <p>this is the entire logcat:</p>
  
  <pre><code>02-09 09:31:35.308    3679-3679/com.example.evelien.domotica D/AndroidRuntime﹕ Shutting down VM
  02-09 09:31:35.308    3679-3679/com.example.evelien.domotica E/AndroidRuntime﹕ FATAL EXCEPTION: main
      Process: com.example.evelien.domotica, PID: 3679
      java.lang.IllegalStateException: Could not execute method of the activity
              at android.view.View$1.onClick(View.java:4020)
              at android.view.View.performClick(View.java:4780)
              at android.view.View$PerformClick.run(View.java:19866)
              at android.os.Handler.handleCallback(Handler.java:739)
              at android.os.Handler.dispatchMessage(Handler.java:95)
              at android.os.Looper.loop(Looper.java:135)
              at android.app.ActivityThread.main(ActivityThread.java:5254)
              at java.lang.reflect.Method.invoke(Native Method)
              at java.lang.reflect.Method.invoke(Method.java:372)
              at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
              at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
       Caused by: java.lang.reflect.InvocationTargetException
              at java.lang.reflect.Method.invoke(Native Method)
              at java.lang.reflect.Method.invoke(Method.java:372)
              at android.view.View$1.onClick(View.java:4015)
              at android.view.View.performClick(View.java:4780)
              at android.view.View$PerformClick.run(View.java:19866)
              at android.os.Handler.handleCallback(Handler.java:739)
              at android.os.Handler.dispatchMessage(Handler.java:95)
              at android.os.Looper.loop(Looper.java:135)
              at android.app.ActivityThread.main(ActivityThread.java:5254)
              at java.lang.reflect.Method.invoke(Native Method)
              at java.lang.reflect.Method.invoke(Method.java:372)
              at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
              at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
       Caused by: android.os.NetworkOnMainThreadException
              at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1147)
              at libcore.io.BlockGuardOs.connect(BlockGuardOs.java:110)
              at libcore.io.IoBridge.connectErrno(IoBridge.java:154)
              at libcore.io.IoBridge.connect(IoBridge.java:122)
              at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:183)
              at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:456)
              at java.net.Socket.connect(Socket.java:882)
              at com.android.okhttp.internal.Platform.connectSocket(Platform.java:174)
              at com.android.okhttp.Connection.connect(Connection.java:152)
              at com.android.okhttp.internal.http.HttpEngine.connect(HttpEngine.java:276)
              at com.android.okhttp.internal.http.HttpEngine.sendRequest(HttpEngine.java:211)
              at com.android.okhttp.internal.http.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:382)
              at com.android.okhttp.internal.http.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:106)
              at com.example.evelien.domotica.Domotica.lihtbulp(Domotica.java:76)
              at java.lang.reflect.Method.invoke(Native Method)
              at java.lang.reflect.Method.invoke(Method.java:372)
              at android.view.View$1.onClick(View.java:4015)
              at android.view.View.performClick(View.java:4780)
              at android.view.View$PerformClick.run(View.java:19866)
              at android.os.Handler.handleCallback(Handler.java:739)
              at android.os.Handler.dispatchMessage(Handler.java:95)
              at android.os.Looper.loop(Looper.java:135)
              at android.app.ActivityThread.main(ActivityThread.java:5254)
              at java.lang.reflect.Method.invoke(Native Method)
              at java.lang.reflect.Method.invoke(Method.java:372)
              at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
              at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
  02-09 09:36:35.393    3679-3679/com.example.evelien.domotica I/Process﹕ Sending signal. PID: 3679 SIG: 9
  </code></pre>
  
  <p>the code refers to conn.connect();</p>