I don't understand why the code doesn't works on Android, it's the same code in Java and it's works on my laptop. I've already search on Google, Stackoverflow, and Youtube, but I didn't find the reason. On all Youtube tutorials, this type of code works on android. Example : https://www.youtube.com/watch?v=lM2vDPPx4Xg or https://www.youtube.com/watch?v=MnmEXqfV5BU
Here is the code :
@Override
protected Void doInBackground(Void... voids) {
try{
Class c=Class.forName("org.postgresql.Driver");
Driver driver=(Driver) c.newInstance();
DriverManager.registerDriver(driver);
String url="jdbc:postgresql://url?sslmode=verify-ca&sslfactory=org.postgresql.ssl.DefaultJavaSSLFactory";
String username="username";
String password="pwd";
this.conn=DriverManager.getConnection(url,username,password);
//statement for make request
this.stmt=this.conn.createStatement();
System.out.println("Connecté !");
conn_load=false;
}catch (Exception e){
System.out.println("Connexion problem");
System.out.println(e.getCause());
}
return null;
}
In the windows terminal the code works but not on android :
Caused by: java.lang.NoClassDefFoundError: Failed resolution of: Ljava/lang/management/ManagementFactory;
at org.postgresql.util.PGPropertyMaxResultBufferParser.adjustResultSize(PGPropertyMaxResultBufferParser.java:200)
at org.postgresql.util.PGPropertyMaxResultBufferParser.parseProperty(PGPropertyMaxResultBufferParser.java:37)
at org.postgresql.core.PGStream.setMaxResultBuffer(PGStream.java:643)
at org.postgresql.core.v3.ConnectionFactoryImpl.tryConnect(ConnectionFactoryImpl.java:102)
at org.postgresql.core.v3.ConnectionFactoryImpl.openConnectionImpl(ConnectionFactoryImpl.java:197)
at org.postgresql.core.ConnectionFactory.openConnection(ConnectionFactory.java:49)
at org.postgresql.jdbc.PgConnection.<init>(PgConnection.java:217)
at org.postgresql.Driver.makeConnection(Driver.java:458)
at org.postgresql.Driver.connect(Driver.java:260)
at java.sql.DriverManager.getConnection(DriverManager.java:569)
at java.sql.DriverManager.getConnection(DriverManager.java:219)
at com.devoteam.recrutement.model.bdd.BDD.doInBackground(BDD.java:54)
at com.devoteam.recrutement.model.bdd.BDD.doInBackground(BDD.java:12)
at android.os.AsyncTask$2.call(AsyncTask.java:333)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:245)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
at java.lang.Thread.run(Thread.java:764)
Caused by: java.lang.ClassNotFoundException: Didn't find class "java.lang.management.ManagementFactory" on path: DexPathList[[zip file "/data/app/com.devoteam.recrutement-B8V00xTM4oetyFeYswlxVQ==/base.apk"],nativeLibraryDirectories=[/data/app/com.devoteam.recrutement-B8V00xTM4oetyFeYswlxVQ==/lib/arm, /system/lib]]
at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:134)
at java.lang.ClassLoader.loadClass(ClassLoader.java:379)
at java.lang.ClassLoader.loadClass(ClassLoader.java:312)
at org.postgresql.util.PGPropertyMaxResultBufferParser.adjustResultSize(PGPropertyMaxResultBufferParser.java:200)
at org.postgresql.util.PGPropertyMaxResultBufferParser.parseProperty(PGPropertyMaxResultBufferParser.java:37)
at org.postgresql.core.PGStream.setMaxResultBuffer(PGStream.java:643)
at org.postgresql.core.v3.ConnectionFactoryImpl.tryConnect(ConnectionFactoryImpl.java:102)
at org.postgresql.core.v3.ConnectionFactoryImpl.openConnectionImpl(ConnectionFactoryImpl.java:197)
at org.postgresql.core.ConnectionFactory.openConnection(ConnectionFactory.java:49)
at org.postgresql.jdbc.PgConnection.<init>(PgConnection.java:217)
at org.postgresql.Driver.makeConnection(Driver.java:458)
at org.postgresql.Driver.connect(Driver.java:260)
at java.sql.DriverManager.getConnection(DriverManager.java:569)
at java.sql.DriverManager.getConnection(DriverManager.java:219)
at com.devoteam.recrutement.model.bdd.BDD.doInBackground(BDD.java:54)
at com.devoteam.recrutement.model.bdd.BDD.doInBackground(BDD.java:12)
at android.os.AsyncTask$2.call(AsyncTask.java:333)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:245)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
at java.lang.Thread.run(Thread.java:764)
java.lang.management.ManagementFactory
). if you want to connect to a database from Android, it is advisable to use a (rest) service (eg written in Java) to mediate between your Android application and the database. – Dunkin