D
D
Dmitry2022-02-20 09:39:04
PostgreSQL
Dmitry, 2022-02-20 09:39:04

Android Studio connect to PostgreSQL?

Good afternoon folks.
I'm trying to connect to postgresql and I seem to be doing everything according to the article (2020), but I constantly catch errors. and somewhat different for different versions of the driver. with implementation 'org.postgresql:postgresql:42.2.5' connection error (moreover, it crashes somewhere in the driver):

code

static final String URL = " jdbc:postgresql://10.0.2.2:5432/db_example?user=so... ";
private PostgreData() {
        try {
            Class.forName("org.postgresql.Driver");
        } catch (ClassNotFoundException e) {
            Log.println(Log.ERROR, "SQL", "TestApp");
            Log.getStackTraceString(e);
            AlertDialog dialog = DialogWnd.getDialog(mainActivity, e.getMessage(), DialogWnd.DLG_ERROR);
            assert dialog != null;
            dialog.show();
            return;
        }

        StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
        StrictMode.setThreadPolicy(policy);

        try {
            connection = DriverManager.getConnection(URL); //DriverManager.getConnection(DB_URL, DB_USER, DB_PASS);
            status = true;
        } catch (SQLException e) {
            status = false;
            Log.println(Log.ERROR, "SQL", "TestApp");
            Log.getStackTraceString(e);
            AlertDialog dialog = DialogWnd.getDialog(mainActivity, e.getMessage(), DialogWnd.DLG_ERROR);
            assert dialog != null;
            dialog.show();
        }
    }

logcat

java.lang.NoClassDefFoundError: Failed resolution of: Ljava/time/Duration;
at org.postgresql.jdbc.TimestampUtils.(TimestampUtils.java:54)
at org.postgresql.jdbc.TimestampUtils.parseBackendTimeZone(TimestampUtils.java:1409)
at org.postgresql.core.v3.QueryExecutorImpl.receiveParameterStatus(QueryExecutorImpl.java:2635)
at org.postgresql.core.v3.QueryExecutorImpl.readStartupMessages(QueryExecutorImpl.java:2568)
at org.postgresql.core.v3.QueryExecutorImpl.(QueryExecutorImpl.java:133)
at org.postgresql.core.v3.ConnectionFactoryImpl.openConnectionImpl(ConnectionFactoryImpl.java:250)
at org.postgresql.core.ConnectionFactory.openConnection(ConnectionFactory.java:49)
at org.postgresql.jdbc.PgConnection.(PgConnection.java:195)
at org.postgresql.Driver.makeConnection(Driver.java:454)
at org.postgresql.Driver.connect(Driver.java:256)
at java.sql.DriverManager.getConnection(DriverManager.java:569)
at java.sql.DriverManager.getConnection(DriverManager.java:237)
at com.example.testapp.PostgreData.(PostgreData.java:64)
at com.example.testapp.PostgreData.getInstance(PostgreData.java:37)
at com.example.testapp.DataShowActivity.onCreate(DataShowActivity.java:26)
at android.app.Activity.performCreate(Activity.java:6679)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2618)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6119)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
Caused by: java.lang.ClassNotFoundException: Didn't find class "java.time.Duration" on path: DexPathList
at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)
at java.lang.ClassLoader.loadClass(ClassLoader.java:380)
at java.lang.ClassLoader.loadClass(ClassLoader.java:312)
at org.postgresql.jdbc.TimestampUtils.(TimestampUtils.java:54) 
at org.postgresql.jdbc.TimestampUtils.parseBackendTimeZone(TimestampUtils.java:1409) 
at org.postgresql.core.v3.QueryExecutorImpl.receiveParameterStatus(QueryExecutorImpl.java:2635) 
at org.postgresql.core.v3.QueryExecutorImpl.readStartupMessages(QueryExecutorImpl.java:2568) 
at org.postgresql.core.v3.QueryExecutorImpl.(QueryExecutorImpl.java:133) 
at org.postgresql.core.v3.ConnectionFactoryImpl.openConnectionImpl(ConnectionFactoryImpl.java:250) 
at org.postgresql.core.ConnectionFactory.openConnection(ConnectionFactory.java:49) 
at org.postgresql.jdbc.PgConnection.(PgConnection.java:195) 
at org.postgresql.Driver.makeConnection(Driver.java:454) 
at org.postgresql.Driver.connect(Driver.java:256) 
at java.sql.DriverManager.getConnection(DriverManager.java:569) 
at java.sql.DriverManager.getConnection(DriverManager.java:237) 
at com.example.testapp.PostgreData.(PostgreData.java:64) 
at com.example.testapp.PostgreData.getInstance(PostgreData.java:37) 
at com.example.testapp.DataShowActivity.onCreate(DataShowActivity.java:26) 
at android.app.Activity.performCreate(Activity.java:6679) 
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118) 
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2618) 
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726) 
at android.app.ActivityThread.-wrap12(ActivityThread.java) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477) 
at android.os.Handler.dispatchMessage(Handler.java:102) 
at android.os.Looper.loop(Looper.java:154) 
at android.app.ActivityThread.main(ActivityThread.java:6119) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776) 

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dmitry, 2022-02-20
@t1gr1s

Perhaps this question can be closed:
Generally speaking most Android/iPhone apps connect to Postgres through a
"middle tier" API written in Ruby, Python, Java, Node.JS or similar. In
some cases you might sync Postgres to a local SQLite DB on the Android
device more directly, but that would be a pretty specialized and uncommon
implementation strategy.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question