0

Android: PostgreSQL connect() verursacht PSQLException

Versuche ich mit meiner Android-App (mit Funktionen, die in einer Wicket-Applikation funktionieren) auf eine Datenbank zuzugreifen, bekomme ich folgenden Fehler:

org.postgresql.util.PSQLException: Something unusual has occured to cause the driver to fail. Please report this exception.

org.postgresql.util.PSQLException: Something unusual has occured to cause the driver to fail. Please report this exception.
at org.postgresql.Driver.connect(Driver.java:287)
at java.sql.DriverManager.getConnection(DriverManager.java:175)
at com.j256.ormlite.jdbc.JdbcConnectionSource.makeConnection(JdbcConnectionSource.java:243)
at com.j256.ormlite.jdbc.JdbcConnectionSource.getReadWriteConnection(JdbcConnectionSource.java:182)
at com.j256.ormlite.jdbc.JdbcConnectionSource.getReadOnlyConnection(JdbcConnectionSource.java:168)
at com.j256.ormlite.stmt.StatementExecutor.buildIterator(StatementExecutor.java:228)
at com.j256.ormlite.stmt.StatementExecutor.query(StatementExecutor.java:181)
at com.j256.ormlite.stmt.StatementExecutor.queryForAll(StatementExecutor.java:113)
at com.j256.ormlite.dao.BaseDaoImpl.queryForAll(BaseDaoImpl.java:239)
at xxxxx
at xxxxx
at android.app.Activity.performCreate(Activity.java:5104)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
at android.app.ActivityThread.access$600(ActivityThread.java:141)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5041)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
at dalvik.system.NativeStart.main(Native Method)
Caused by: android.os.NetworkOnMainThreadException
at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1117)
at java.net.InetAddress.lookupHostByName(InetAddress.java:385)
at java.net.InetAddress.getAllByNameImpl(InetAddress.java:236)
at java.net.InetAddress.getByName(InetAddress.java:289)
at java.net.InetSocketAddress.(InetSocketAddress.java:105)
at java.net.InetSocketAddress.(InetSocketAddress.java:90)
at org.postgresql.core.PGStream.(PGStream.java:60)
at org.postgresql.core.v3.ConnectionFactoryImpl.openConnectionImpl(ConnectionFactoryImpl.java:101)
at org.postgresql.core.ConnectionFactory.openConnection(ConnectionFactory.java:64)
at org.postgresql.jdbc2.AbstractJdbc2Connection.(AbstractJdbc2Connection.java:136)
at org.postgresql.jdbc3.AbstractJdbc3Connection.(AbstractJdbc3Connection.java:29)
at org.postgresql.jdbc3g.AbstractJdbc3gConnection.(AbstractJdbc3gConnection.java:21)
at org.postgresql.jdbc4.AbstractJdbc4Connection.(AbstractJdbc4Connection.java:31)
at org.postgresql.jdbc4.Jdbc4Connection.(Jdbc4Connection.java:24)
at org.postgresql.Driver.makeConnection(Driver.java:393)
at org.postgresql.Driver.connect(Driver.java:267)
… 24 more

und daraus resultierend eine NullPointerException:

Uncaught exception thrown by finalizer
java.lang.NullPointerException
at org.postgresql.jdbc2.AbstractJdbc2Connection.close(AbstractJdbc2Connection.java:651)
at org.postgresql.jdbc2.AbstractJdbc2Connection.finalize(AbstractJdbc2Connection.java:1003)
at java.lang.Daemons$FinalizerDaemon.doFinalize(Daemons.java:187)
at java.lang.Daemons$FinalizerDaemon.run(Daemons.java:170)
at java.lang.Thread.run(Thread.java:856)

Wie ich herausgefunden habe, liegt es an der Android SDK Version. Ab der Version 9 dürfen nämlich UI-Threads keine Verbindungen mit dem Internet mehr aufbauen, wenn man es ihnen nicht explizit erlaubt.

Lösung:
Am Anfang der onCreate()-Methode folgendes einfügen:

if (android.os.Build.VERSION.SDK_INT >= 9) 
{		
    StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitNetwork().build();
    StrictMode.setThreadPolicy(policy);
}


Schreibe einen Kommentar

Deine E-Mail-Adresse wird nicht veröffentlicht. Erforderliche Felder sind mit * markiert.