title: Connecting to PostgreSQL through JDBC - NetworkOnMainThreadException
body: <p>I have finally managed to access postgres but the method is not according to the requirement. through first method i can connect but through second method i receive below error.</p>
  
  <p>Here is my code.</p>
  
  <pre><code>public class MainActivity extends Activity {
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
  
          Button login = (Button) findViewById(R.id.Connect);
          login.setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View v) {
                  //new JSONTask().execute("file:///C:/Users/intel/Desktop/details.json");
                  System.out.println("Connection Start");
                  new PostgreSqlJDBC().execute();
              }
          });
      }
  
      public class PostgreSqlJDBC extends AsyncTask&lt;Void, Void, Void&gt; {
          @Override
          public Void doInBackground(Void... params) {
              Connection conexion = null;
              System.out.println("Connection Intitializing");
              try {
                  Class.forName("org.postgresql.Driver");
                  String url="jdbc:postgresql://192.168.0.5:5432/Taxi";
                  conexion = DriverManager.getConnection(url, "admin", "123");
                  System.out.println("Connection Successfull");
              } catch (Exception e) {
                  e.printStackTrace();
                  System.err.println(e.getMessage());
                  System.err.println("Error: Cant connect!");
  
                  System.err.println("----- PostgreSQL query ends correctly!-----");
                  return null;
              }
              return null;
          }
  
  
      }
  
  }
  </code></pre>
  
  <p>But i want to access it through this way. the CONNECT method is reference to a button in xml.</p>
  
  <pre><code> public class MainActivity extends Activity {
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
      }
  
      public void Connect(View v) {
          Connection conexion = null;
          System.out.println("Connection Intitializing");
          try {
              Class.forName("org.postgresql.Driver");
              String url="jdbc:postgresql://192.168.0.5:5432/Taxi";
              conexion = DriverManager.getConnection(url, "admin", "123");
              System.out.println("Connection Successfull");
          } catch (Exception e) {
              e.printStackTrace();
              System.err.println(e.getMessage());
              System.err.println("Error: Cant connect!");
  
              System.err.println("----- PostgreSQL query ends correctly!-----");
  
          }
      }
  
  
  }
  </code></pre>
  
  <p>Log output in the console. </p>
  
  <pre><code>    09-13 00:08:10.990    6948-6948/? I/System.out﹕ Connection Intitializing
  09-13 00:08:10.993    1233-1294/? W/AudioTrack﹕ AUDIO_OUTPUT_FLAG_FAST denied by client
  09-13 00:08:11.079    6948-6948/? W/System.err﹕ org.postgresql.util.PSQLException: Something unusual has occurred to cause the driver to fail. Please report this exception.
  09-13 00:08:11.079    6948-6948/? W/System.err﹕ at org.postgresql.Driver.connect(Driver.java:295)
  09-13 00:08:11.079    6948-6948/? W/System.err﹕ at java.sql.DriverManager.getConnection(DriverManager.java:179)
  09-13 00:08:11.079    6948-6948/? W/System.err﹕ at java.sql.DriverManager.getConnection(DriverManager.java:213)
  09-13 00:08:11.079    6948-6948/? W/System.err﹕ at com.echotaxicab.MainActivity.Connect(MainActivity.java:42)
  09-13 00:08:11.079    6948-6948/? W/System.err﹕ at java.lang.reflect.Method.invoke(Native Method)
  09-13 00:08:11.079    6948-6948/? W/System.err﹕ at java.lang.reflect.Method.invoke(Method.java:372)
  09-13 00:08:11.079    6948-6948/? W/System.err﹕ at android.view.View$1.onClick(View.java:4015)
  09-13 00:08:11.079    6948-6948/? W/System.err﹕ at android.view.View.performClick(View.java:4780)
  09-13 00:08:11.079    6948-6948/? W/System.err﹕ at android.view.View$PerformClick.run(View.java:19866)
  09-13 00:08:11.079    6948-6948/? W/System.err﹕ at android.os.Handler.handleCallback(Handler.java:739)
  09-13 00:08:11.079    6948-6948/? W/System.err﹕ at android.os.Handler.dispatchMessage(Handler.java:95)
  09-13 00:08:11.080    6948-6948/? W/System.err﹕ at android.os.Looper.loop(Looper.java:135)
  09-13 00:08:11.080    6948-6948/? W/System.err﹕ at android.app.ActivityThread.main(ActivityThread.java:5257)
  09-13 00:08:11.080    6948-6948/? W/System.err﹕ at java.lang.reflect.Method.invoke(Native Method)
  09-13 00:08:11.080    6948-6948/? W/System.err﹕ at java.lang.reflect.Method.invoke(Method.java:372)
  09-13 00:08:11.080    6948-6948/? W/System.err﹕ at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
  09-13 00:08:11.080    6948-6948/? W/System.err﹕ at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
  09-13 00:08:11.080    6948-6948/? W/System.err﹕ Caused by: android.os.NetworkOnMainThreadException
  09-13 00:08:11.080    6948-6948/? W/System.err﹕ at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1147)
  09-13 00:08:11.080    6948-6948/? W/System.err﹕ at libcore.io.BlockGuardOs.connect(BlockGuardOs.java:110)
  09-13 00:08:11.080    6948-6948/? W/System.err﹕ at libcore.io.IoBridge.connectErrno(IoBridge.java:154)
  09-13 00:08:11.080    6948-6948/? W/System.err﹕ at libcore.io.IoBridge.connect(IoBridge.java:122)
  09-13 00:08:11.080    6948-6948/? W/System.err﹕ at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:183)
  09-13 00:08:11.081    6948-6948/? W/System.err﹕ at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:456)
  09-13 00:08:11.081    6948-6948/? W/System.err﹕ at java.net.Socket.connect(Socket.java:882)
  09-13 00:08:11.081    6948-6948/? W/System.err﹕ at org.postgresql.core.PGStream.&lt;init&gt;(PGStream.java:64)
  09-13 00:08:11.081    6948-6948/? W/System.err﹕ at org.postgresql.core.v3.ConnectionFactoryImpl.openConnectionImpl(ConnectionFactoryImpl.java:147)
  09-13 00:08:11.081    6948-6948/? W/System.err﹕ at org.postgresql.core.ConnectionFactory.openConnection(ConnectionFactory.java:54)
  09-13 00:08:11.081    6948-6948/? W/System.err﹕ at org.postgresql.jdbc.PgConnection.&lt;init&gt;(PgConnection.java:218)
  09-13 00:08:11.081    6948-6948/? W/System.err﹕ at org.postgresql.Driver.makeConnection(Driver.java:407)
  09-13 00:08:11.081    6948-6948/? W/System.err﹕ at org.postgresql.Driver.connect(Driver.java:275)
  09-13 00:08:11.081    6948-6948/? W/System.err﹕ ... 16 more
  09-13 00:08:11.081    6948-6948/? W/System.err﹕ Something unusual has occurred to cause the driver to fail. Please report this exception.
  09-13 00:08:11.081    6948-6948/? W/System.err﹕ Error: Cant connect!
  09-13 00:08:11.081    6948-6948/? W/System.err﹕ ----- PostgreSQL query ends correctly!-----
  </code></pre>