title: Android app force close when sending data to local webserver from Android
body: <p><br/>
  I want to send data to local webserver.<br/>
  i use <code>OutputStreamWriter</code> to make a request to webserver</p>
  
  <p>here is my code</p>
  
  <pre><code>protected void trySubmit(Editable d1, Editable d2, Editable d3)
  {
      HttpURLConnection connection;
      OutputStreamWriter request = null;
  
      URL url = null;
      String response = null;
      String parameters = "fullname="+d1+"&amp;email="+d2+"&amp;password="+d3;   
      //Toast.makeText(this,"Message from Server: \n"+ parameters, 0).show();
  
      try
      {
          url = new URL("http://10.0.2.2:8080/labAndroid/submit.php");
          connection = (HttpURLConnection) url.openConnection();
          connection.setDoOutput(true);
          connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
          connection.setRequestMethod("POST");    
  
          request = new OutputStreamWriter(connection.getOutputStream());
          request.write(parameters);
          request.flush();
          request.close();            
          String line = "";               
          InputStreamReader stream_reader = new InputStreamReader(connection.getInputStream());
          BufferedReader reader = new BufferedReader(stream_reader);
          StringBuilder sb = new StringBuilder();
          while ((line = reader.readLine()) != null)
          {
              sb.append(line + "\n");
          }
          // Response from server after login process will be stored in response variable.                
          response = sb.toString();
          // You can perform UI operations here
          Toast.makeText(this,"Message from Server: \n"+ response, 0).show();             
          stream_reader.close();
          reader.close();
      }
      catch(IOException e)
      {
          Toast.makeText(this,"Error :?", 0).show(); 
      }
  
  }
  </code></pre>
  
  <p>but when i clicked on this method, my app got a force close.
  and here is my logcat</p>
  
  <pre><code>11-16 05:36:09.094: E/AndroidRuntime(1461): FATAL EXCEPTION: main
  11-16 05:36:09.094: E/AndroidRuntime(1461): Process: com.satryacode.sendtointernet, PID: 1461
  11-16 05:36:09.094: E/AndroidRuntime(1461): android.os.NetworkOnMainThreadException
  11-16 05:36:09.094: E/AndroidRuntime(1461):     at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1145)
  11-16 05:36:09.094: E/AndroidRuntime(1461):     at libcore.io.BlockGuardOs.connect(BlockGuardOs.java:84)
  11-16 05:36:09.094: E/AndroidRuntime(1461):     at libcore.io.IoBridge.connectErrno(IoBridge.java:127)
  11-16 05:36:09.094: E/AndroidRuntime(1461):     at libcore.io.IoBridge.connect(IoBridge.java:112)
  11-16 05:36:09.094: E/AndroidRuntime(1461):     at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:192)
  11-16 05:36:09.094: E/AndroidRuntime(1461):     at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:459)
  11-16 05:36:09.094: E/AndroidRuntime(1461):     at java.net.Socket.connect(Socket.java:843)
  11-16 05:36:09.094: E/AndroidRuntime(1461):     at com.android.okhttp.internal.Platform.connectSocket(Platform.java:131)
  11-16 05:36:09.094: E/AndroidRuntime(1461):     at com.android.okhttp.Connection.connect(Connection.java:101)
  11-16 05:36:09.094: E/AndroidRuntime(1461):     at com.android.okhttp.internal.http.HttpEngine.connect(HttpEngine.java:294)
  11-16 05:36:09.094: E/AndroidRuntime(1461):     at com.android.okhttp.internal.http.HttpEngine.sendSocketRequest(HttpEngine.java:255)
  11-16 05:36:09.094: E/AndroidRuntime(1461):     at com.android.okhttp.internal.http.HttpEngine.sendRequest(HttpEngine.java:206)
  11-16 05:36:09.094: E/AndroidRuntime(1461):     at com.android.okhttp.internal.http.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:345)
  11-16 05:36:09.094: E/AndroidRuntime(1461):     at com.android.okhttp.internal.http.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:89)
  11-16 05:36:09.094: E/AndroidRuntime(1461):     at com.android.okhttp.internal.http.HttpURLConnectionImpl.getOutputStream(HttpURLConnectionImpl.java:197)
  11-16 05:36:09.094: E/AndroidRuntime(1461):     at com.satryacode.sendtointernet.MainActivity.trySubmit(MainActivity.java:66)
  11-16 05:36:09.094: E/AndroidRuntime(1461):     at com.satryacode.sendtointernet.MainActivity$1.onClick(MainActivity.java:43)
  11-16 05:36:09.094: E/AndroidRuntime(1461):     at android.view.View.performClick(View.java:4424)
  11-16 05:36:09.094: E/AndroidRuntime(1461):     at android.view.View$PerformClick.run(View.java:18383)
  11-16 05:36:09.094: E/AndroidRuntime(1461):     at android.os.Handler.handleCallback(Handler.java:733)
  11-16 05:36:09.094: E/AndroidRuntime(1461):     at android.os.Handler.dispatchMessage(Handler.java:95)
  11-16 05:36:09.094: E/AndroidRuntime(1461):     at android.os.Looper.loop(Looper.java:137)
  11-16 05:36:09.094: E/AndroidRuntime(1461):     at android.app.ActivityThread.main(ActivityThread.java:4998)
  11-16 05:36:09.094: E/AndroidRuntime(1461):     at java.lang.reflect.Method.invokeNative(Native Method)
  11-16 05:36:09.094: E/AndroidRuntime(1461):     at java.lang.reflect.Method.invoke(Method.java:515)
  11-16 05:36:09.094: E/AndroidRuntime(1461):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:777)
  11-16 05:36:09.094: E/AndroidRuntime(1461):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:593)
  11-16 05:36:09.094: E/AndroidRuntime(1461):     at dalvik.system.NativeStart.main(Native Method)
  </code></pre>
  
  <p>is there anyone know how to fix that? Thank you :)</p>