title: Android: Application has stopped unexpectedly! FATAL EXCEPTION: main NetworkOnMainThreadException
body: <p>When I use my android phone to sent json to the server and the application is creashed. But it works when I use emulator.</p>
  
  <pre><code>Application has stopped unexpectedly!
  </code></pre>
  
  <p>Code:</p>
  
  <pre><code>String url = &lt;&lt;the url&gt;&gt;;
  DefaultHttpClient httpClient = new DefaultHttpClient();
  
  HttpPost httpPost = new HttpPost(url);
  
  
  List&lt;NameValuePair&gt; params = new ArrayList&lt;NameValuePair&gt;();
  params.add(new BasicNameValuePair("email", email));
  params.add(new BasicNameValuePair("password", password));
  
  try {
    httpPost.setEntity(new UrlEncodedFormEntity(params));
                            httpPost.getParams().setParameter(CoreProtocolPNames.USE_EXPECT_CONTINUE, Boolean.FALSE);
                      } catch (UnsupportedEncodingException e1) {
                          // TODO Auto-generated catch block
                          e1.printStackTrace();
                      }
  
                      HttpResponse httpResponse = null;
                      StringBuilder builder = new StringBuilder();
                      JSONTokener tokener = null;
                      try {
                          httpResponse = httpClient.execute(httpPost);
                          HttpEntity entity = httpResponse.getEntity();
  
                          InputStream content = entity.getContent();
  
                          BufferedReader reader = new BufferedReader(new InputStreamReader(content));
      String line;
  
      while((line = reader.readLine()) != null){
      builder.append(line);
      }
      tokener = new JSONTokener(builder.toString());
      String execution = null;
  
      try{
      //convert the api response into json object
      JSONObject jsonObject = new JSONObject(tokener);
      execution = jsonObject.get("execution").toString();
  
      //display the json object
      } catch(Exception e){
      e.printStackTrace();
      }
  
      if (execution.equals("true")){
      //run method to open message dialog
      openSuccessDialog(v, "You have logged in your account.");
      }
      else{
      //run method to open message dialog
      openErrorDialog(v, "Execution false. Your email and password doesn't match.");
                          }
  </code></pre>
  
  <p>LogCat:</p>
  
  <pre><code>08-01 10:26:52.985: W/dalvikvm(4335): threadid=1: thread exiting with uncaught exception (group=0x416a7e18)
  08-01 10:26:52.995: E/AndroidRuntime(4335): FATAL EXCEPTION: main
  08-01 10:26:52.995: E/AndroidRuntime(4335): Process: com.entityaccess.assetmappingsprintone, PID: 4335
  08-01 10:26:52.995: E/AndroidRuntime(4335): android.os.NetworkOnMainThreadException
  08-01 10:26:52.995: E/AndroidRuntime(4335):     at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1239)
  08-01 10:26:52.995: E/AndroidRuntime(4335):     at libcore.io.BlockGuardOs.connect(BlockGuardOs.java:84)
  08-01 10:26:52.995: E/AndroidRuntime(4335):     at libcore.io.IoBridge.connectErrno(IoBridge.java:127)
  08-01 10:26:52.995: E/AndroidRuntime(4335):     at libcore.io.IoBridge.connect(IoBridge.java:112)
  08-01 10:26:52.995: E/AndroidRuntime(4335):     at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:192)
  08-01 10:26:52.995: E/AndroidRuntime(4335):     at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:459)
  08-01 10:26:52.995: E/AndroidRuntime(4335):     at java.net.Socket.connect(Socket.java:873)
  08-01 10:26:52.995: E/AndroidRuntime(4335):     at org.apache.http.conn.scheme.PlainSocketFactory.connectSocket(PlainSocketFactory.java:125)
  08-01 10:26:52.995: E/AndroidRuntime(4335):     at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:144)
  08-01 10:26:52.995: E/AndroidRuntime(4335):     at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
  08-01 10:26:52.995: E/AndroidRuntime(4335):     at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
  08-01 10:26:52.995: E/AndroidRuntime(4335):     at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:367)
  08-01 10:26:52.995: E/AndroidRuntime(4335):     at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:754)
  08-01 10:26:52.995: E/AndroidRuntime(4335):     at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:519)
  08-01 10:26:52.995: E/AndroidRuntime(4335):     at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:497)
  08-01 10:26:52.995: E/AndroidRuntime(4335):     at com.entityaccess.assetmappingsprintone.LoginActivity$1.onClick(LoginActivity.java:119)
  08-01 10:26:52.995: E/AndroidRuntime(4335):     at android.view.View.performClick(View.java:4480)
  08-01 10:26:52.995: E/AndroidRuntime(4335):     at android.view.View$PerformClick.run(View.java:18686)
  08-01 10:26:52.995: E/AndroidRuntime(4335):     at android.os.Handler.handleCallback(Handler.java:733)
  08-01 10:26:52.995: E/AndroidRuntime(4335):     at android.os.Handler.dispatchMessage(Handler.java:95)
  08-01 10:26:52.995: E/AndroidRuntime(4335):     at android.os.Looper.loop(Looper.java:157)
  08-01 10:26:52.995: E/AndroidRuntime(4335):     at android.app.ActivityThread.main(ActivityThread.java:5872)
  08-01 10:26:52.995: E/AndroidRuntime(4335):     at java.lang.reflect.Method.invokeNative(Native Method)
  08-01 10:26:52.995: E/AndroidRuntime(4335):     at java.lang.reflect.Method.invoke(Method.java:515)
  08-01 10:26:52.995: E/AndroidRuntime(4335):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:858)
  08-01 10:26:52.995: E/AndroidRuntime(4335):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:674)
  08-01 10:26:52.995: E/AndroidRuntime(4335):     at dalvik.system.NativeStart.main(Native Method)
  08-01 10:26:55.858: D/Process(4335): killProcess, pid=4335
  08-01 10:26:55.858: D/Process(4335): com.android.internal.os.RuntimeInit$UncaughtHandler.uncaughtException:131 java.lang.ThreadGroup.uncaughtException:693 java.lang.ThreadGroup.uncaughtException:690 
  </code></pre>
  
  <p>How can I solve this to make my phone be able to send json to the server?</p>