Yusuf Koçak Yusuf Koçak - 3 years ago 275
Android Question

Android postgresql connection

I want to connect to the database on my computer from my phone. I use JDBC for this. But I can not make a connection. I can connect to the database with a different SQL Client application. I have added the postgresql jdbc driver project.

try {
Class.forName("org.postgresql.Driver");
Connection connection = null;
connection = DriverManager.getConnection("jdbc:postgresql://192.168.1.40:5432/postgres","postgres","1");
connection.setAutoCommit(false);

} catch (SQLException e) {
txt.setText("Connection failed :"+e.getMessage());

e.printStackTrace();
} catch (ClassNotFoundException e) {
txt.setText("Library not found");

e.printStackTrace();
}

}
});


Error code:

E/org.postgresql.Driver: Unexpected connection error:
android.os.NetworkOnMainThreadException
at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1156)
at libcore.io.BlockGuardOs.connect(BlockGuardOs.java:84)
at libcore.io.IoBridge.connectErrno(IoBridge.java:159)
at libcore.io.IoBridge.connect(IoBridge.java:112)
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:192)
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:475)
at java.net.Socket.connect(Socket.java:861)
at org.postgresql.core.PGStream.<init>(PGStream.java:62)
at org.postgresql.core.v3.ConnectionFactoryImpl.openConnectionImpl(ConnectionFactoryImpl.java:144)
at org.postgresql.core.ConnectionFactory.openConnection(ConnectionFactory.java:49)
at org.postgresql.jdbc.PgConnection.<init>(PgConnection.java:194)
at org.postgresql.Driver.makeConnection(Driver.java:431)
at org.postgresql.Driver.connect(Driver.java:247)
at java.sql.DriverManager.getConnection(DriverManager.java:179)
at java.sql.DriverManager.getConnection(DriverManager.java:144)
at yube.com.databaseconnection.MainActivity$1.onClick(MainActivity.java:37)
at android.view.View.performClick(View.java:4463)
at android.view.View$PerformClick.run(View.java:18789)
at android.os.Handler.handleCallback(Handler.java:808)
at android.os.Handler.dispatchMessage(Handler.java:103)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:5307)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:831)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:647)
at dalvik.system.NativeStart.main(Native Method)


Can you help me.

Answer Source

The exception says that you try to establish a network connection on the main thread. This is not allowed in strict mode because this would make your app unresponsive.

You have two options:

  1. Use a new Thread or AsyncTask to connect to your database

  2. Modify the thread policy (only recommended to test the connection process)

    StrictMode.setThreadPolicy(StrictMode.ThreadPolicy.Builder().permitAll().build())

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download