1

Well, trying to access a postgresql Database using Android, I got the following, very surprising Exception:

    org.postgresql.util.PSQLException: Something unusual has occurred to cause the driver to fail. Please report this exception.
        at org.postgresql.Driver.connect(Driver.java:305)
        at java.sql.DriverManager.getConnection(DriverManager.java:179)
        at java.sql.DriverManager.getConnection(DriverManager.java:213)
        at org.pbdevelopement.cineapp.NetReader.getConnection(NetReader.java:42)
        at org.pbdevelopement.cineapp.MainActivity.getPersonalMovies(MainActivity.java:242)
        at org.pbdevelopement.cineapp.MainActivity.onCreate(MainActivity.java:115)
        at android.app.Activity.performCreate(Activity.java:6034)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1111)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2332)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2442)
        at android.app.ActivityThread.access$800(ActivityThread.java:156)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1351)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:211)
        at android.app.ActivityThread.main(ActivityThread.java:5373)
        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:1020)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:815)
    Caused by: android.os.NetworkOnMainThreadException
        at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1151)
        at java.net.InetAddress.lookupHostByName(InetAddress.java:418)
        at java.net.InetAddress.getAllByNameImpl(InetAddress.java:252)
        at java.net.InetAddress.getByName(InetAddress.java:305)
        at java.net.InetSocketAddress.(InetSocketAddress.java:105)
        at java.net.InetSocketAddress.(InetSocketAddress.java:90)
        at org.postgresql.core.PGStream.(PGStream.java:61)
        at org.postgresql.core.v3.ConnectionFactoryImpl.openConnectionImpl(ConnectionFactoryImpl.java:129)
        at org.postgresql.core.ConnectionFactory.openConnection(ConnectionFactory.java:65)
        at org.postgresql.jdbc.PgConnection.(PgConnection.java:159)
        at org.postgresql.Driver.makeConnection(Driver.java:415)
        at org.postgresql.Driver.connect(Driver.java:283)
        ... 18 more

I have no clue how to fix this, neither do I have a clue where to report it to... Does anybody have any ideas?

Pedantic
  • 5,032
  • 2
  • 24
  • 37
Maverick283
  • 1,284
  • 3
  • 16
  • 33

1 Answers1

4

It seems that you are trying to perform your database request through a network operation on the applications Main Thread. This throws the exception android.os.NetworkOnMainThreadException because every network operation in Android must be started in an own thread. Try to perform your Database request in AsyncTask.

AsyncTask

Community
  • 1
  • 1
Verdion
  • 56
  • 2
  • Agreed. Check your code that's listed in the stack trace: `at org.pbdevelopement.cineapp.NetReader.getConnection(NetReader.java:42) at org.pbdevelopement.cineapp.MainActivity.getPersonalMovies(MainActivity.java:242) at org.pbdevelopement.cineapp.MainActivity.onCreate(MainActivity.java:115)` and implement somewhere besides your main thread. – Pedantic Feb 19 '16 at 16:31
  • Hm, sounds reasonable... Should it not throw a different exception though? – Maverick283 Feb 20 '16 at 16:22