MongoCredential credential = MongoCredential.createCredential(user, database, password);
MongoClientOptions options = MongoClientOptions.builder().sslEnabled(true).build();
MongoClient mongoClient = new MongoClient(new ServerAddress("localhost", 27017), Arrays.asList(credential), options);
抛出以下错误
07-05 12:24:00.127 11721-11879/com.example.michael.version_10 W/org.bson.ObjectId: Failed to get process identifier from JMX, using random number instead
java.lang.NoClassDefFoundError: Failed resolution of: Ljava/lang/management/ManagementFactory;
at org.bson.types.ObjectId.createProcessIdentifier(ObjectId.java:506)
at org.bson.types.ObjectId.<clinit>(ObjectId.java:464)
at com.mongodb.connection.ClusterId.<init>(ClusterId.java:47)
at com.mongodb.connection.DefaultClusterFactory.create(DefaultClusterFactory.java:69)
at com.mongodb.Mongo.createCluster(Mongo.java:683)
at com.mongodb.Mongo.createCluster(Mongo.java:669)
at com.mongodb.Mongo.<init>(Mongo.java:282)
at com.mongodb.MongoClient.<init>(MongoClient.java:187)
at com.example.michael.version_10.Connection.doInBackground(Connection.java:40)
at com.example.michael.version_10.Connection.doInBackground(Connection.java:30)
at android.os.AsyncTask$2.call(AsyncTask.java:305)
at java.util.concurrent.FutureTask.run(FutureTask.java:237)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:243)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1133)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:607)
at java.lang.Thread.run(Thread.java:761)
Caused by: java.lang.ClassNotFoundException: Didn't find class "java.lang.management.ManagementFactory" on path: DexPathList[[zip file "/data/app/com.example.michael.version_10-1/base.apk", zip file "/data/app/com.example.michael.version_10-1/split_lib_dependencies_apk.apk", zip file "/data/app/com.example.michael.version_10-1/split_lib_slice_0_apk.apk", zip file "/data/app/com.example.michael.version_10-1/split_lib_slice_1_apk.apk", zip file "/data/app/com.example.michael.version_10-1/split_lib_slice_2_apk.apk", zip file "/data/app/com.example.michael.version_10-1/split_lib_slice_3_apk.apk", zip file "/data/app/com.example.michael.version_10-1/split_lib_slice_4_apk.apk", zip file "/data/app/com.example.michael.version_10-1/split_lib_slice_5_apk.apk", zip file "/data/app/com.example.michael.version_10-1/split_lib_slice_6_apk.apk", zip file "/data/app/com.example.michael.version_10-1/split_lib_slice_7_apk.apk", zip file "/data/app/com.example.michael.version_10-1/split_lib_slice_8_apk.apk", zip file "/data/app/com.example.michael.version_10-1/split_lib_slice_9_apk.apk"],nativeLibraryDirectories=[/data/app/com.example.michael.version_10-1/lib/x86, /system/lib, /vendor/lib]]
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.bson.types.ObjectId.createProcessIdentifier(ObjectId.java:506)
at org.bson.types.ObjectId.<clinit>(ObjectId.java:464)
at com.mongodb.connection.ClusterId.<init>(ClusterId.java:47)
at com.mongodb.connection.DefaultClusterFactory.create(DefaultClusterFactory.java:69)
at com.mongodb.Mongo.createCluster(Mongo.java:683)
at com.mongodb.Mongo.createCluster(Mongo.java:669)
at com.mongodb.Mongo.<init>(Mongo.java:282)
at com.mongodb.MongoClient.<init>(MongoClient.java:187)
at com.example.michael.version_10.Connection.doInBackground(Connection.java:40)
at com.example.michael.version_10.Connection.doInBackground(Connection.java:30)
at android.os.AsyncTask$2.call(AsyncTask.java:305)
at java.util.concurrent.FutureTask.run(FutureTask.java:237)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:243)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1133)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:607)
at java.lang.Thread.run(Thread.java:761)
答案 0 :(得分:0)
我实际上做错了。不建议直接连接到MongoDB,建议使用Rest API。这解决了我的问题。